<commit_before>﻿(* ------------------------------------------------------------------------
This file is part of fszmq.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------------ *)
namespace fszmq

open System

[<Experimental "This module is not properly compatible with languages other than F#">]
module Options =

  type [<Measure>] bit
  type [<Measure>] b = bit
  type [<Measure>] kilobit
  type [<Measure>] kb = kilobit
  type [<Measure>] second = FSharp.Data.UnitSystems.SI.UnitNames.second
  type [<Measure>] s = second
  type [<Measure>] millisecond
  type [<Measure>] ms = millisecond
  type [<Measure>] Byte
  type [<Measure>] B = Byte
  type [<Measure>] NetworkHop
  type [<Measure>] hop = NetworkHop
  
  /// Socket options
  type SocketOption =
    /// I/O thread affinity bit-mask
    | Affinity of bitmask:uint64
    /// Socket identifier
    | Identity of identifier:byte[]
    /// Add subscription filter
    | Subscribe of filter:byte[]
    /// Remove subscription filter
    | Unsubscribe of filter:byte[]
    /// Multicast data rate
    | Rate of dataRate:int<kb/s>
    /// Multicast recovery period
    | MulticastRecovery of time:int<ms>
    /// Send-message buffer size in bytes
    | SendBuffer of size:int<Byte>
    /// Receive-message buffer size in bytes
    | ReceiveBuffer of int<Byte>
    /// More message frames available
    | MoreMessageFramesAvailable of bool
    /// Socket event state, see all: Polling
    | Events of polling:int
    /// Pause before shutdown
    | LingerDelay of delay:int<ms>
    /// Pause before reconnect
    | ReconnectDelay of delay:int<ms>
    /// Maximum number of queued peers
    | Backlog of length:int
    /// Maximum reconnection interval
    | MaxReconnectInterval of delay:int<ms>
    /// Maximum inbound message size
    | MaxMessageSize of size:int64<Byte>
    /// Maximum number of outbound queued messages
    | SendQueue of size:int
    /// Maximum number of inbound queued messages
    | ReceiveQueue of size:int
    /// Time-to-live for each multicast packet
    | MulticastHops of hops:int<NetworkHop>
    /// Timeout period for inbound messages
    | ReceiveTimeout of timeout:int<ms> 
    /// Timeout period for outbound messages
    | SendTimeout of timeout:int<ms>
    /// true to error on unroutable messages, false to silently ignore
    | RouterMandatory of errorOnUnroutable:bool
    /// Override OS-level TCP keep-alive
    | TcpKeepalive of enable:bool option
    /// Override OS-level TCP keep-alive
    | TcpKeepaliveCount of count:int
    /// Override OS-level TCP keep-alive
    | TcpKeepaliveIdle of idle:int<s>
    /// Override OS-level TCP keep-alive
    | TcpKeepaliveInterval of delay:int<s>
    /// limit queuing to only completed connections
    | Immediate of queueCompletedOnly:bool
    /// resend duplicate messages
    | ResendDuplicateMessages of resendDuplicate:bool
    /// true to enable IPv6 on the socket, false to restrict to only IPv4
    | Ipv6 of ipv6:bool
    /// make socket act as server for PLAIN security
    | PlainServer of enabled:bool
    /// Set user name and password for client using PLAIN security
    | PlainClient of username:string * password:<PASSWORD>
    /// Make socket act as a server for CURVE security
    | CurveServer of enabled:bool * secretKey:byte[]  
    /// Public and private keys fo client using CURVE security
    | CurveClient of publicKey:byte[] * secretKey:byte[] * serverPublicKey:string 
    /// automatically send an empty message on new connection
    | ProbeRouter of sendEmptyMessageOnConnection:bool
    /// prefix messages with explicit request ID
    | RequestCorrelation of prefixExplicitRequestId:bool
    /// relax strict alternation between ZMQ.REQ and ZMQ.REP
    | RelaxStrictAlternation of strictAlternation:bool
    /// keep last message in queue (ignores high-water mark options)
    | KeepLastMessageInQueue of keepMessage:bool
    /// Sets authentication domain
    | AuthenticationDomain of domain:string
    
    //TODO: LAST_ENDPOINT
    //TODO: ROUTER_HANDOVER
    //TODO: TOS
    //TODO: CONNECT_RID
    //TODO: GSSAPI_SERVER
    //TODO: GSSAPI_PRINCIPAL
    //TODO: GSSAPI_SERVICE_PRINCIPAL
    //TODO: GSSAPI_PLAINTEXT
    //TODO: HANDSHAKE_IVL
    //TODO: SOCKS_PROXY
    //TODO: XPUB_NODROP
  
  [<AutoOpen;CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  module SocketOption =
        
    let setSocketOption socket option =
      match option with
      // general options
      | Affinity                    mask        -> Socket.setOption socket (ZMQ.AFFINITY            ,mask           )
      | Identity                    identifier  -> Socket.setOption socket (ZMQ.IDENTITY            ,identifier     )
      | Subscribe                   filter      -> Socket.setOption socket (ZMQ.SUBSCRIBE           ,filter         )
      | Unsubscribe                 filter      -> Socket.setOption socket (ZMQ.UNSUBSCRIBE         ,filter         )
      | Rate                        dataRate    -> Socket.setOption socket (ZMQ.RATE                ,dataRate       )
      | MulticastRecovery           time        -> Socket.setOption socket (ZMQ.RECOVERY_IVL        ,time           )
      | SendBuffer                  size        -> Socket.setOption socket (ZMQ.SNDBUF              ,size           )
      | ReceiveBuffer               size        -> Socket.setOption socket (ZMQ.RCVBUF              ,size           )
      | MoreMessageFramesAvailable  more        -> Socket.setOption socket (ZMQ.RCVMORE             ,more           )
      | Events                      events      -> Socket.setOption socket (ZMQ.EVENTS              ,events         )    
      | AuthenticationDomain        domain      -> Socket.setOption socket (ZMQ.ZAP_DOMAIN          ,domain         )
      | Backlog                     length      -> Socket.setOption socket (ZMQ.BACKLOG             ,length         )
      | Immediate                   true        -> Socket.setOption socket (ZMQ.IMMEDIATE           ,1              )
      | Immediate                   false       -> Socket.setOption socket (ZMQ.IMMEDIATE           ,0              )
      | Ipv6                        true        -> Socket.setOption socket (ZMQ.IPV6                ,1              )
      | Ipv6                        false       -> Socket.setOption socket (ZMQ.IPV6                ,0              )
      | KeepLastMessageInQueue      true        -> Socket.setOption socket (ZMQ.CONFLATE            ,1              )
      | KeepLastMessageInQueue      false       -> Socket.setOption socket (ZMQ.CONFLATE            ,0              )
      | LingerDelay                 delay       -> Socket.setOption socket (ZMQ.LINGER              ,delay          )
      | MaxMessageSize              size        -> Socket.setOption socket (ZMQ.MAXMSGSIZE          ,size           )
      | MaxReconnectInterval        delay       -> Socket.setOption socket (ZMQ.RECONNECT_IVL_MAX   ,delay          )
      | MulticastHops               hops        -> Socket.setOption socket (ZMQ.MULTICAST_HOPS      ,hops           )
      | ProbeRouter                 true        -> Socket.setOption socket (ZMQ.PROBE_ROUTER        ,1              )
      | ProbeRouter                 false       -> Socket.setOption socket (ZMQ.PROBE_ROUTER        ,0              )
      | ReceiveQueue                size        -> Socket.setOption socket (ZMQ.RCVHWM              ,size           )
      | ReceiveTimeout              delay       -> Socket.setOption socket (ZMQ.RCVTIMEO            ,delay          )
      | ReconnectDelay              delay       -> Socket.setOption socket (ZMQ.RECONNECT_IVL       ,delay          )
      | RelaxStrictAlternation      true        -> Socket.setOption socket (ZMQ.REQ_RELAXED         ,1              )
      | RelaxStrictAlternation      false       -> Socket.setOption socket (ZMQ.REQ_RELAXED         ,0              )
      | RequestCorrelation          true        -> Socket.setOption socket (ZMQ.REQ_CORRELATE       ,1              )
      | RequestCorrelation          false       -> Socket.setOption socket (ZMQ.REQ_CORRELATE       ,0              )
      | ResendDuplicateMessages     true        -> Socket.setOption socket (ZMQ.XPUB_VERBOSE        ,1              )
      | ResendDuplicateMessages     false       -> Socket.setOption socket (ZMQ.XPUB_VERBOSE        ,0              )
      | RouterMandatory             true        -> Socket.setOption socket (ZMQ.ROUTER_MANDATORY    ,1              )
      | RouterMandatory             false       -> Socket.setOption socket (ZMQ.ROUTER_MANDATORY    ,0              )
      | SendQueue                   size        -> Socket.setOption socket (ZMQ.SNDHWM              ,size           )
      | SendTimeout                 delay       -> Socket.setOption socket (ZMQ.SNDTIMEO            ,delay          )
      | TcpKeepalive                (None      )-> Socket.setOption socket (ZMQ.TCP_KEEPALIVE       ,-1             )
      | TcpKeepalive                (Some false)-> Socket.setOption socket (ZMQ.TCP_KEEPALIVE       , 0             )
      | TcpKeepalive                (Some true )-> Socket.setOption socket (ZMQ.TCP_KEEPALIVE       ,+1             )
      | TcpKeepaliveCount           count       -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE_CNT   ,count          )
      | TcpKeepaliveIdle            idle        -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE_IDLE  ,idle           )
      | TcpKeepaliveInterval        delay       -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE_INTVL ,delay          )
      // PLAIN security
      | PlainServer true      ->  Socket.setOption socket (ZMQ.PLAIN_SERVER   ,1  )
      | PlainServer false     ->  Socket.setOption socket (ZMQ.PLAIN_SERVER   ,0  )
      | PlainClient (unm,pwd) ->  Socket.setOption socket (ZMQ.PLAIN_USERNAME ,unm)
                                  Socket.setOption socket (ZMQ.PLAIN_PASSWORD ,pwd)    
      // CURVE security
      | CurveServer (true ,secretKey)               ->  Socket.setOption socket (ZMQ.CURVE_SERVER   ,1        )
                                                        Socket.setOption socket (ZMQ.CURVE_SECRETKEY,secretKey)
      | CurveServer (false,_        )               ->  Socket.setOption socket (ZMQ.CURVE_SERVER   ,0        )
      | CurveClient (publicKey,secretKey,serverKey) ->  Socket.setOption socket (ZMQ.CURVE_PUBLICKEY,publicKey)
                                                        Socket.setOption socket (ZMQ.CURVE_SECRETKEY,secretKey)
                                                        Socket.setOption socket (ZMQ.CURVE_SERVERKEY,serverKey)
  
    let configureSocket socket socketOptions = 
      Seq.iter (setSocketOption socket) socketOptions<commit_msg>Collapse bool cases in setSocketOption.<commit_after>﻿(* ------------------------------------------------------------------------
This file is part of fszmq.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
------------------------------------------------------------------------ *)
namespace fszmq

open System

[<Experimental "This module is not properly compatible with languages other than F#">]
module Options =

  type [<Measure>] bit
  type [<Measure>] b = bit
  type [<Measure>] kilobit
  type [<Measure>] kb = kilobit
  type [<Measure>] second = FSharp.Data.UnitSystems.SI.UnitNames.second
  type [<Measure>] s = second
  type [<Measure>] millisecond
  type [<Measure>] ms = millisecond
  type [<Measure>] Byte
  type [<Measure>] B = Byte
  type [<Measure>] NetworkHop
  type [<Measure>] hop = NetworkHop
  
  /// Socket options
  type SocketOption =
    /// I/O thread affinity bit-mask
    | Affinity of bitmask:uint64
    /// Socket identifier
    | Identity of identifier:byte[]
    /// Add subscription filter
    | Subscribe of filter:byte[]
    /// Remove subscription filter
    | Unsubscribe of filter:byte[]
    /// Multicast data rate
    | Rate of dataRate:int<kb/s>
    /// Multicast recovery period
    | MulticastRecovery of time:int<ms>
    /// Send-message buffer size in bytes
    | SendBuffer of size:int<Byte>
    /// Receive-message buffer size in bytes
    | ReceiveBuffer of int<Byte>
    /// More message frames available
    | MoreMessageFramesAvailable of bool
    /// Socket event state, see all: Polling
    | Events of polling:int
    /// Pause before shutdown
    | LingerDelay of delay:int<ms>
    /// Pause before reconnect
    | ReconnectDelay of delay:int<ms>
    /// Maximum number of queued peers
    | Backlog of length:int
    /// Maximum reconnection interval
    | MaxReconnectInterval of delay:int<ms>
    /// Maximum inbound message size
    | MaxMessageSize of size:int64<Byte>
    /// Maximum number of outbound queued messages
    | SendQueue of size:int
    /// Maximum number of inbound queued messages
    | ReceiveQueue of size:int
    /// Time-to-live for each multicast packet
    | MulticastHops of hops:int<NetworkHop>
    /// Timeout period for inbound messages
    | ReceiveTimeout of timeout:int<ms> 
    /// Timeout period for outbound messages
    | SendTimeout of timeout:int<ms>
    /// true to error on unroutable messages, false to silently ignore
    | RouterMandatory of errorOnUnroutable:bool
    /// Override OS-level TCP keep-alive
    | TcpKeepalive of enable:bool option
    /// Override OS-level TCP keep-alive
    | TcpKeepaliveCount of count:int
    /// Override OS-level TCP keep-alive
    | TcpKeepaliveIdle of idle:int<s>
    /// Override OS-level TCP keep-alive
    | TcpKeepaliveInterval of delay:int<s>
    /// limit queuing to only completed connections
    | Immediate of queueCompletedOnly:bool
    /// resend duplicate messages
    | ResendDuplicateMessages of resendDuplicate:bool
    /// true to enable IPv6 on the socket, false to restrict to only IPv4
    | Ipv6 of ipv6:bool
    /// make socket act as server for PLAIN security
    | PlainServer of enabled:bool
    /// Set user name and password for client using PLAIN security
    | PlainClient of username:string * password:string
    /// Make socket act as a server for CURVE security
    | CurveServer of enabled:bool * secretKey:byte[]  
    /// Public and private keys fo client using CURVE security
    | CurveClient of publicKey:byte[] * secretKey:byte[] * serverPublicKey:string 
    /// automatically send an empty message on new connection
    | ProbeRouter of sendEmptyMessageOnConnection:bool
    /// prefix messages with explicit request ID
    | RequestCorrelation of prefixExplicitRequestId:bool
    /// relax strict alternation between ZMQ.REQ and ZMQ.REP
    | RelaxStrictAlternation of strictAlternation:bool
    /// keep last message in queue (ignores high-water mark options)
    | KeepLastMessageInQueue of keepMessage:bool
    /// Sets authentication domain
    | AuthenticationDomain of domain:string
    
    //TODO: LAST_ENDPOINT
    //TODO: ROUTER_HANDOVER
    //TODO: TOS
    //TODO: CONNECT_RID
    //TODO: GSSAPI_SERVER
    //TODO: GSSAPI_PRINCIPAL
    //TODO: GSSAPI_SERVICE_PRINCIPAL
    //TODO: GSSAPI_PLAINTEXT
    //TODO: HANDSHAKE_IVL
    //TODO: SOCKS_PROXY
    //TODO: XPUB_NODROP
  
  [<AutoOpen;CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  module SocketOption =
        
    let setSocketOption socket option =
      match option with
      // general options
      | Affinity                    mask        -> Socket.setOption socket (ZMQ.AFFINITY            ,mask           )
      | Identity                    identifier  -> Socket.setOption socket (ZMQ.IDENTITY            ,identifier     )
      | Subscribe                   filter      -> Socket.setOption socket (ZMQ.SUBSCRIBE           ,filter         )
      | Unsubscribe                 filter      -> Socket.setOption socket (ZMQ.UNSUBSCRIBE         ,filter         )
      | Rate                        dataRate    -> Socket.setOption socket (ZMQ.RATE                ,dataRate       )
      | MulticastRecovery           time        -> Socket.setOption socket (ZMQ.RECOVERY_IVL        ,time           )
      | SendBuffer                  size        -> Socket.setOption socket (ZMQ.SNDBUF              ,size           )
      | ReceiveBuffer               size        -> Socket.setOption socket (ZMQ.RCVBUF              ,size           )
      | MoreMessageFramesAvailable  more        -> Socket.setOption socket (ZMQ.RCVMORE             ,more           )
      | Events                      events      -> Socket.setOption socket (ZMQ.EVENTS              ,events         )    
      | AuthenticationDomain        domain      -> Socket.setOption socket (ZMQ.ZAP_DOMAIN          ,domain         )
      | Backlog                     length      -> Socket.setOption socket (ZMQ.BACKLOG             ,length         )
      | Immediate                   immediate   -> Socket.setOption socket (ZMQ.IMMEDIATE           ,immediate      )
      | Ipv6                        ipv6        -> Socket.setOption socket (ZMQ.IPV6                ,ipv6           )
      | KeepLastMessageInQueue      keep        -> Socket.setOption socket (ZMQ.CONFLATE            ,keep           )
      | LingerDelay                 delay       -> Socket.setOption socket (ZMQ.LINGER              ,delay          )
      | MaxMessageSize              size        -> Socket.setOption socket (ZMQ.MAXMSGSIZE          ,size           )
      | MaxReconnectInterval        delay       -> Socket.setOption socket (ZMQ.RECONNECT_IVL_MAX   ,delay          )
      | MulticastHops               hops        -> Socket.setOption socket (ZMQ.MULTICAST_HOPS      ,hops           )
      | ProbeRouter                 router      -> Socket.setOption socket (ZMQ.PROBE_ROUTER        ,router         )
      | ReceiveQueue                size        -> Socket.setOption socket (ZMQ.RCVHWM              ,size           )
      | ReceiveTimeout              delay       -> Socket.setOption socket (ZMQ.RCVTIMEO            ,delay          )
      | ReconnectDelay              delay       -> Socket.setOption socket (ZMQ.RECONNECT_IVL       ,delay          )
      | RelaxStrictAlternation      relax       -> Socket.setOption socket (ZMQ.REQ_RELAXED         ,relax          )
      | RequestCorrelation          corr        -> Socket.setOption socket (ZMQ.REQ_CORRELATE       ,corr           )
      | ResendDuplicateMessages     resend      -> Socket.setOption socket (ZMQ.XPUB_VERBOSE        ,resend         )
      | RouterMandatory             mandatory   -> Socket.setOption socket (ZMQ.ROUTER_MANDATORY    ,mandatory      )
      | SendQueue                   size        -> Socket.setOption socket (ZMQ.SNDHWM              ,size           )
      | SendTimeout                 delay       -> Socket.setOption socket (ZMQ.SNDTIMEO            ,delay          )
      | TcpKeepalive                (Some keep) -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE       ,keep           )
      | TcpKeepalive                sysDefault  -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE       ,sysDefault     )
      | TcpKeepaliveCount           count       -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE_CNT   ,count          )
      | TcpKeepaliveIdle            idle        -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE_IDLE  ,idle           )
      | TcpKeepaliveInterval        delay       -> Socket.setOption socket (ZMQ.TCP_KEEPALIVE_INTVL ,delay          )
      // PLAIN security
      | PlainServer true      ->  Socket.setOption socket (ZMQ.PLAIN_SERVER   ,1  )
      | PlainServer false     ->  Socket.setOption socket (ZMQ.PLAIN_SERVER   ,0  )
      | PlainClient (unm,pwd) ->  Socket.setOption socket (ZMQ.PLAIN_USERNAME ,unm)
                                  Socket.setOption socket (ZMQ.PLAIN_PASSWORD ,pwd)    
      // CURVE security
      | CurveServer (true ,secretKey)               ->  Socket.setOption socket (ZMQ.CURVE_SERVER   ,1        )
                                                        Socket.setOption socket (ZMQ.CURVE_SECRETKEY,secretKey)
      | CurveServer (false,_        )               ->  Socket.setOption socket (ZMQ.CURVE_SERVER   ,0        )
      | CurveClient (publicKey,secretKey,serverKey) ->  Socket.setOption socket (ZMQ.CURVE_PUBLICKEY,publicKey)
                                                        Socket.setOption socket (ZMQ.CURVE_SECRETKEY,secretKey)
                                                        Socket.setOption socket (ZMQ.CURVE_SERVERKEY,serverKey)
  
    let configureSocket socket socketOptions = 
      Seq.iter (setSocketOption socket) socketOptions
{'fraction_non_alphanumeric': 0.09024179620034542, 'fraction_numerical': 0.002830550758011898, 'mean_word_length': 1.6869038411961845, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 147, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7619188', 'n_tokens_mistral': 5922, 'n_tokens_neox': 5612, 'n_words': 1502}