LDAP / Scope in database code (3)

I have an upcoming project that will require a custom LDAP server backed by an existing inventory/asset management application, which runs on Oracle. Of course, I plan to tackle this in OCaml. I’m confident enough with Oracaml now, and I’m delighted to find that OCaml’s LDAP module includes a server API. So to construct a new LDAP server, the process seems to be very straightforward:

  1. Implement a function (Ldap_funserver.connection_id → Ldap_types.ldap_message → unit → Ldap_types.ldap_message) to actually do the lookup and return the result. This is the only functionality I require at this stage – the underlying data will be managed by the existing application, so no need for add, modify, delete and so on. I don’t even need authentication since it will run on trusted network. This function wll get the request in an ldap_message and construct an ldap_message with the reply.
  2. Create a record of type backendInfo populating bi_op_search with my new function. All fields of this type are optional.
  3. Pass this record to init, and get back a record of type server_info.
  4. Pass that to run and we’re ready to start accepting clients. Perhaps I’ll put in a signal handler for a clean shutdown too.

Here’s where it gets more fiddly. I can also implement bi_init (unit → unit) which is called as the server starts up (and there is a corresponding bi_close called when shutting down). These are the logical places to place occi_connect and occi_disconnect respectively, and occi_create_statement in the function in step (1) with the lda created therein. What I don’t understand is how to get that from there and into my handler function, without using global mutable state.

I am starting to think that as and when I start implementing my own APIs I will always include an arbitary type in the method signature, that the developer can just set to () if they don’t care, a sort of poor man’s Reader monad

About Gaius

Jus' a good ol' boy, never meanin' no harm
This entry was posted in Ocaml, Oracle. Bookmark the permalink.

4 Responses to LDAP / Scope in database code (3)

  1. Gaius says:

    Perhaps the way to do this is to skip bi_init, connect to Oracle first and then give bi_op_search a curried function, which has already been applied to the connection… And give a similar function to bi_close.

  2. David House says:

    I don’t think the mutable state need be global; it is sufficient that the closure you pass as the bi_init function closes over it. I.e.:

    let () =
      let occi_connection = ref None in
      let bi_init () = ... in
      let bi_close () = ... in
      ...
    

    You can keep that scope small by calling out to functions from a higher-up scope, passing the state explicitly. That seems decidedly less evil.

  3. Pingback: LDAP (2) | So I decided to take my work back underground

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s