1/**
2 * \file  block_server_client.c
3 * \brief block server client domain
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#include <stdio.h>
16#include <string.h>
17
18#include <barrelfish/barrelfish.h>
19#include <barrelfish/sys_debug.h>
20#include <bench/bench.h>
21
22/* includes for ip address and lwip wrappers */
23#include <lwip/inet.h>
24#include <lwip/init.h>
25
26/* bulk transfer facilities */
27#include <bulk_transfer/bulk_transfer.h>
28#include <bulk_transfer/bulk_allocator.h>
29
30/* local includes of block server client */
31#include "block_server.h"
32#include "local_server.h"
33#include "network_common.h"
34#include "network_client.h"
35
36#if (BLOCK_ENABLE_NETWORKING == 0)
37#include "block_storage.h"
38#endif // (RUN_NET == 0)
39
40/**
41 * \brief Contains the connection parameters to the network block server
42 */
43static struct block_net_service block_server;
44
45/* ======================= run net test facility =========================== */
46
47static void message_handler_loop(void)
48{
49    errval_t err;
50    struct waitset *ws = get_default_waitset();
51    while (true) {
52        err = event_dispatch_non_block(ws);
53        if (err != LIB_ERR_NO_EVENT) {
54            if (err_is_fail(err)) {
55                DEBUG_ERR(err, "in event_dispatch");
56                break;
57            }
58        }
59
60        wrapper_perform_lwip_work();
61        /*err = event_dispatch(ws);
62         if (err_is_fail(err)) {
63         DEBUG_ERR(err, "in event_dispatch");
64         break;
65         }*/
66    }
67}
68
69
70/**
71 * \brief Main function for the network block server client.
72 *
73 */
74int main(int argc, char *argv[])
75{
76    debug_printf("Block service network client started.\n");
77
78    errval_t err;
79
80    /*
81     * Connect to the network block server
82     */
83
84#if BLOCK_ENABLE_NETWORKING
85    if (argc != 2) {
86        USER_PANIC("usage: %s [IP]", argv[0]);
87    }
88
89    struct ip_addr server_ip;
90    struct in_addr addr;
91    if (!inet_aton(argv[1], &addr)) {
92        USER_PANIC("Could not convert the ip address");
93    }
94
95    /* XXX: does not work in ipv6 */
96    server_ip.addr = addr.s_addr;
97
98    err = block_net_connect(&block_server, &server_ip, BLOCK_NET_PORT);
99    if (err_is_fail(err)) {
100        USER_PANIC_ERR(err, "could not connect to the block server");
101        exit(EXIT_FAILURE);
102    }
103
104#if BLOCK_BENCH_ENABLE
105    run_test(&block_server.tx_chan, &block_server.rx_chan, &block_server);
106    debug_printf("test executed\n");
107    while (1)
108        ;
109    assert(!"should not reach that point");
110    return EXIT_SUCCESS;
111#endif
112#endif
113    /*
114     * TODO: Setup block caching
115     */
116
117    /*
118     * Initialize the core local server (flounder interface)
119     */
120#if BLOCK_ENABLE_NETWORKING
121    uint32_t flags = SERVICE_FLAG_CLIENT;
122#else
123    uint32_t flags = SERVICE_FLAG_DEFAULT;
124
125    err = block_storage_init(BLOCK_COUNT, BLOCK_SIZE);
126    if (err_is_fail(err)) {
127        USER_PANIC_ERR(err, "could not initialize the block service.\n");
128        exit(EXIT_FAILURE);
129    }
130#endif // RUN_NET
131
132    err = block_local_init(&block_server, flags);
133    if (err_is_fail(err)) {
134        /* disconnect from the network server */
135        block_net_disconnect(&block_server);
136        USER_PANIC_ERR(err, "could not initialize local service");
137        exit(EXIT_FAILURE);
138    }
139
140    err = block_local_start();
141    if (err_is_fail(err)) {
142        block_net_disconnect(&block_server);
143        USER_PANIC_ERR(err, "could not start the local server.\n");
144        exit(EXIT_FAILURE);
145    }
146
147    message_handler_loop();
148}
149