/*
 *    Copyright 2004-2006 Intel Corporation
 * 
 *    Licensed 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.
 */

#ifndef _APISERVER_H_
#define _APISERVER_H_

#include <oasys/debug/Log.h>
#include <oasys/thread/Thread.h>
#include <oasys/io/TCPClient.h>
#include <oasys/io/TCPServer.h>

#include "dtn_api.h"
#include "dtn_ipc.h"
#include "dtn_types.h"

namespace dtn {

class APIRegistration;
class APIRegistrationList;

/**
 * Class that implements the main server side handling of the DTN
 * application IPC.
 */
class APIServer : public oasys::TCPServerThread {
public:
    /**
     * The constructor checks for environment variable overrides of
     * the address / port. It is expected that someone else will call
     * bind_listen_start() on the APIServer instance.
     */
    APIServer();

    // Virtual from TCPServerThread
    void accepted(int fd, in_addr_t addr, u_int16_t port);
    
    in_addr_t  local_addr() const { return local_addr_; }
    in_addr_t* local_addr_ptr() { return &local_addr_; }
    
    u_int16_t  local_port() const { return local_port_; }
    u_int16_t* local_port_ptr() { return &local_port_; }
    
private:
    in_addr_t local_addr_;    ///< local address to bind to
    u_int16_t local_port_;    ///< local port to use for api
};

/**
 * Class that implements the API session.
 */
class APIClient : public oasys::Thread, public oasys::TCPClient {
public:
    APIClient(int fd, in_addr_t remote_host, u_int16_t remote_port);
    virtual ~APIClient();
    virtual void run();

    void close_session();
    
protected:
    int handle_handshake();
    int handle_local_eid();
    int handle_register();
    int handle_unregister();
    int handle_find_registration();
    int handle_bind();
    int handle_unbind();
    int handle_send();
    int handle_recv();
    int handle_begin_poll();
    int handle_cancel_poll();
    int handle_close();

    // wait for a bundle arrival on any bound registration, or for
    // traffic on the api socket.
    //
    // returns the oasys IO error code if there was a timeout or an
    // internal error. returns 0 if there is a bundle waiting or
    // socket data on the channel, and assigns the reg or sock_ready
    // pointers appropriately
    int wait_for_bundle(const char* operation, dtn_timeval_t timeout,
                        APIRegistration** reg, bool* sock_ready);

    int send_response(int ret);

    bool is_bound(u_int32_t regid);
    
    char buf_[DTN_MAX_API_MSG];
    XDR xdr_encode_;
    XDR xdr_decode_;
    APIRegistrationList* bindings_;
    oasys::Notifier notifier_;
};

} // namespace dtn

#endif /* _APISERVER_H_ */

