<commit_before>﻿// --debois, Mar '15
module Server

// REST key-value store. 

let (|Prefix|_|) (p:string) (s:string) =
    if s.StartsWith(p) then
        Some(s.Substring(p.Length))
    else
        None

type Node = {
    
}
 

[<EntryPoint>]
let main args = 
    let port = 8080;
    use hl = new System.Net.HttpListener ()
    hl.Prefixes.Add <| sprintf "http://+:%d/" port
    // Host '+' means 'accept any path on this path.
    // https://msdn.microsoft.com/en-us/library/system.net.httplistenerprefixcollection.add(v=vs.110).aspx
    hl.Start ()
    printfn "Listener up @ 0.0.0.0:%d" port

   
    let rec loop (store : Map<string, string>) = 
        let cxt      = hl.GetContext()
        let request  = cxt.Request
        let response = cxt.Response
        let meth     = request.HttpMethod
        let path     = request.RawUrl
        let rip      = request.RemoteEndPoint.Address
        let rport    = request.RemoteEndPoint.Port
        let body = 
            use is = new System.IO.StreamReader(request.InputStream, request.ContentEncoding)
            is.ReadToEnd()

        printfn "%s %s from %s %d [%s]" meth path (rip.ToString()) rport body

        let reply answer status reason store' = 
            printfn " --> %d %s [%s]" status reason answer
            // Set HTTP statuscode and reason (e.g., 200 OK)<commit_msg>Node initial setup<commit_after>﻿// --debois, Mar '15
module Server

// REST key-value store. 

let (|Prefix|_|) (p:string) (s:string) =
    if s.StartsWith(p) then
        Some(s.Substring(p.Length))
    else
        None

type Relation =             // The internal string is a http address
    | Condition of string   // What is enabled once a node is executed
    | Exclusion of string   // What becomes excluded once this node is executed
    | Response of string    // What becomes pending once a node is executed

type Node = {
    executed: bool;
    included: bool;
    pending: bool;
    relations: Relation list;
    dependencies: Map<string, bool>; // Whether each condition is fulfilled
    // Ex: {"register": false}
}

// Add a relation to the node
let addRelation node relation =
    failwith "Not implemented"

// Add a dependency to the node
let addDependency node dependency =
    failwith "Not Implemented"

// Notify the node that a dependency has been executed
let setExecuted node dependency =
    failwith  "Not Implemented"

// Notify the node that it has become pending
let setPending node =
    failwith  "Not Implemented"

// Handles when the node is executed
let tryExecute node =
    let rec notify remainingRelations =
        failwith  "Not Implemented"
    failwith  "Not Implemented"

// Returns the status of the node (Should this be here?)
// Note: This should not return the node
let getStatus node =
    failwith  "Not Implemented"

// Ezcludes the node
let exclude node =
    failwith  "Not Implemented"

[<EntryPoint>]
let main args = 
    let name = 
        match args with
        | [||] -> failwith "No name argument given for the node"
        | [|arg|] -> arg
        | _ -> failwith "Too many arguments"
        
    printfn "Starting node '%s'!" name
    let port = 8080;
    use hl = new System.Net.HttpListener ()
    hl.Prefixes.Add <| sprintf "http://+:%d/" port
    // Host '+' means 'accept any path on this path.
    // https://msdn.microsoft.com/en-us/library/system.net.httplistenerprefixcollection.add(v=vs.110).aspx
    hl.Start ()
    printfn "Listener up @ 0.0.0.0:%d" port
    
    let mut state: Node = {
        executed = false;
        included = true;
        pending = false;
        relations = [];
        dependencies = Map.empty;
    }
   
    let rec loop (store : Map<string, string>) = 
        let cxt      = hl.GetContext()
        let request  = cxt.Request
        let response = cxt.Response
        let meth     = request.HttpMethod
        let path     = request.RawUrl
        let rip      = request.RemoteEndPoint.Address
        let rport    = request.RemoteEndPoint.Port
        let body = 
            use is = new System.IO.StreamReader(request.InputStream, request.ContentEncoding)
            is.ReadToEnd()

        printfn "%s %s from %s %d [%s]" meth path (rip.ToString()) rport body

        let reply answer status reason store' = 
            printfn " --> %d %s [%s]" status reason answer
            // Set HTTP statuscode and reason (e.g., 200 OK)
{'fraction_non_alphanumeric': 0.10555050045495905, 'fraction_numerical': 0.007279344858962694, 'mean_word_length': 2.2212454212454213, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '787829', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1291, 'n_words': 445}