/* 
 * File:   JuliusServer.h
 * Author: Sunao HARA
 *
 * Created on 2013/08/21, 19:29
 */

#ifndef JULIUSSERVER_H
#define	JULIUSSERVER_H

#include <stdio.h>
#include <netinet/in.h>

#include <event2/event.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>

//#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include "Julius.h"
#include "IJuliusOutput.h"

#define JULIUS_OUTPUT_SERVER_PORT 10500

namespace Junius {

typedef class CJuliusServer CJuliusServer;

typedef struct _JuliusServerCBData {
    class CJulius            *julius;
    CJuliusServer      *joutput;
    struct event_base  *evbase;
    struct bufferevent *evbuf;
} JuliusServerCBData;

class CJuliusServer : public IJuliusOutput {
private:
    struct event_base     *m_ev_base;
    struct evconnlistener *m_ev_listener;
    struct sockaddr_in    m_sin;
    
    JuliusServerCBData m_cbData;
    

//    boost::thread_group thread_manager;

public:
    CJulius* m_julius;
    CJuliusServer(int argc, char* argv[]);
    //CJuliusServer(const CJuliusServer& orig);
    virtual ~CJuliusServer();
    
    void run();
    void runAsyncThread();
    void waitAsyncThread();
    void terminateAsyncThread();
    
    static void CallbackEvAccept(struct evconnlistener *listener,
        evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx);
    static void CallbackEvError(struct evconnlistener *listener, void *ctx);

    static void CallbackEvRead(struct bufferevent *bev, void *ctx);
    static void CallbackEvWrite(struct bufferevent *bev, void *ctx);
    static void CallbackEvEvent(struct bufferevent *bev, short events, void *ctx);
    
    // IJuliusOutput
    bool onData(char* text);
private:

};

} // namespace
#endif	/* JULIUSSERVER_H */

