/*
 *
 * 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.
 *
 */


// This file automatically generated by:
//   Apache Etch 1.1.0-incubating (LOCAL-0) / c 1.1.0-incubating (LOCAL-0)
//   Fri Aug 28 15:58:20 CEST 2009
// This file is automatically created and should not be edited!
/*
 * helloworld_client_main.c
 */
 
#include "helloworld_client_main.h"
#include "etch_objecttypes.h"
#include "etch_runtime.h"
#include "etch_arrayval.h"
#include "etch_nativearray.h"
#include "etch_binary_tdo.h"
#include "etch_general.h"


/**
 * new_helloworld_client().
 * callback constructor for client implementation object.
 * this callback address is passed to start_helloworld_client() in [main].
 * @param server the remote server. 
 * @remarks this callback must be supplied, i.e. its functionality cannot be 
 * defaulted, since the client implementation constructor new_helloworld_client_impl()
 * is not known to start_helloworld_client().
 */
static i_helloworld_client* helloworld_client_create(void* factory_thisx, helloworld_remote_server* server)
{
    helloworld_client_impl* client = new_helloworld_client_impl(server);
    return client? client->helloworld_client_base:NULL;
}


/**
 * main()
 */
int main(int argc, char* argv[])
{
    etch_status_t etch_status    = ETCH_SUCCESS;
    helloworld_remote_server* remote = NULL;
    int waitupms = 4000;
    etch_string* result = NULL;
    helloworld_user* user = NULL;

    wchar_t* uri = L"tcp://127.0.0.1:4001";	
	
       etch_config_t* config = NULL;
    etch_config_create(&config);
    etch_config_set_property(config, "etch.mailbox.timeout.read", "1000");
    // set properties or read file

    etch_status = etch_runtime_initialize(config);
    if(etch_status != ETCH_SUCCESS) {
        // error
        return 1;
    }

    etch_status = helloworld_helper_remote_server_create(&remote, uri, NULL, helloworld_client_create);
    if(etch_status != ETCH_SUCCESS) {
        // error
    }

    etch_status = helloworld_helper_remote_server_start_wait(remote, waitupms);
    if(etch_status != ETCH_SUCCESS) {
        // error
    }
	
    user = new_helloworld_user();
    user->id = 5;
    user->name = new_stringw(L"User");
    result = remote->say_hello(remote, user);
    if(is_etch_exception(result)) {
        etch_exception* ex = (etch_exception*)result;
        etch_string* ex_string = etch_exception_get_message(ex);
        printf("Exception: %S\n",ex_string->v.valw);
    }


    printf("%S\n",result->v.valw);
    etch_object_destroy(result);
    
    // wait for keypress
    waitkey();
    
    etch_status = helloworld_helper_remote_server_stop_wait(remote, waitupms);
    if(etch_status != ETCH_SUCCESS) {
        // error
    }

    etch_status = helloworld_helper_remote_server_destroy(remote);
    if(etch_status != ETCH_SUCCESS) {
        // error
    }

    etch_status = etch_runtime_shutdown();
    if(etch_status != ETCH_SUCCESS) {
        // error
    }

    etch_config_destroy(config);
    return 0;
}

