/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "dworker.h"
#include <stdio.h>
#include "apr_strings.h"

static int handler_example_add(dw_req_t* dwr, char* data, apr_size_t len, void* baton)
{
    char reply[128];
    int a = 0;
    int b = 0;
    int rv = 0;
 
    /* XXXXX: error logging */
    if (len < 1) {
        return -1;
    }
    
    rv = sscanf(data, "%d,%d", &a, &b);
    fprintf(stderr, "input: %s ==  %d+%d\n", data, a, b);

    /* XXXXX: error logging */
    if (rv != 2) {
        return -1;
    }

    rv = a+b;
    len = apr_snprintf(reply, sizeof(reply), "%d", rv);
    
    fprintf(stderr, "sending: %s\n", reply);
    dw_req_send_result(dwr, reply, len);
    
    return 0;
}

static void register_example(dw_ctxt_t *dwc, dw_module_t *dmod)
{
    dw_register_handler(dwc, dmod, "EX_ADD", handler_example_add, NULL);    
}

DW_MODULE_DECL(example, register_example);