<commit_before>/* Copyright (c) 2016, <NAME>. All Rights Reserved.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0.
 */

#include <time.h>

#include "rvi.h"
#include "jansson.h"

#define BUFSIZE 256
#define LEN 10

void callbackFunc(int fd, void *service_data, json_t *parameters);

void waitFor(unsigned int secs);

void processChoice( int choice );
 
void listChoices(void); 

void initialize(void);

void connect(void);

void disconnect(void);

void get_connections(void);

void register_service(void);

void unregister_service(void);

void get_services(void);

void invoke(void);

void process(void);

void shutdown(void);

rvi_handle myHandle = {0};

int main(int argc, char *argv[])
{
    int choice;

    listChoices();

    while ( scanf("%d", &choice) ) {
        processChoice( choice );
        listChoices();
    }

    return 0;
}

void callbackFunc(int fd, void *service_data, json_t *parameters)
{
    printf("inside the callback function\n");
}

void waitFor(unsigned int secs) 
{
    unsigned int retTime = time(0) + secs;  /* Get end time */
    while( time(0) < retTime);              /* Loop until it arrives */
}

void processChoice( int choice ) 
{
    switch( choice ) {
        case 0:
            printf("You chose to initialize RVI.\n");
            initialize();
            break;
        case 1:
            printf("You chose to connect to a remote node.\n");
            int stat = rvi_connect(myHandle, "192.168.18.76", "9007");
            printf("stat after connect is %d\n", stat);
            //connect();
            break;
        case 2:
            printf("Which RVI node would you like to disconnect from?\n");
            disconnect();
            break;
        case 3:
            printf("You chose to get a list of connections.\n");
            get_connections();
            break;
        case 4:
            printf("You chose to register a service.\n");
            register_service();
            break;
        case 5:
            printf("You chose to unregister a service.\n");
            unregister_service();
            break;
        case 6:
            printf("You chose to get a list of services.\n");
            get_services();
            break;
        case 7:
            printf("What service would you like to invoke?\n");
            invoke();
            break;
        case 8:
            printf("You chose to process input.\n");
            process();
            break;
        case 9:
            printf("You chose to shutdown RVI.\n");
            shutdown();
            break;
        case 10:
            printf("Goodbye!\n");
            exit(0);
        default:
            printf("That's not a valid choice.\n");
            break;
    }
}

void listChoices(void) {
    printf("Make a choice:\n"
            "\t[0]: Initialize RVI\n"
            "\t[1]: Connect to remote node\n"
            "\t[2]: Disconnect from remote node\n"
            "\t[3]: Get a list of connections\n"
            "\t[4]: Register a service\n"
            "\t[5]: Unregister a service\n"
            "\t[6]: Get a list of services\n"
            "\t[7]: Invoke remote service\n"
            "\t[8]: Process input\n"
            "\t[9]: Shutdown RVI\n"
            "\t[10]: Close program.\n");
}

void initialize(void)
{
    char input[BUFSIZE] = {0};
    printf("Config file: ");
    scanf("%s", input);
    myHandle = rvi_init(input);
    if( !myHandle ) exit(1);
    printf("RVI initialized!\n");
}

void connect(void)
{
    int stat = rvi_connect(myHandle, "192.168.18.76", "9007");
    if( stat > 0 ) {
        printf("Connected to remote node on fd %d!\n", stat);
    } else {
        printf("Failed to connect to remote node.\n");
    }
    printf("stat after connect is %d\n", stat);
}

void disconnect(void)
{
    int fd;
    scanf("%d", &fd);
    int stat = rvi_disconnect(myHandle, fd);
    printf("stat after disconnecting file descriptor %d is %d\n", fd, stat);
}

void get_connections(void)
{
    int *connections = malloc( LEN * sizeof(int *) );
    int len = LEN;
    int *conn = connections;
    
    for(int i = 0; i < len; i++) {
        connections[i] = 0;
    }

    int stat = rvi_get_connections(myHandle, connections, &len);

    printf("stat after get connections is %d\n", stat);<commit_msg>[interactive] Fix weird socket issue.<commit_after>/* Copyright (c) 2016, <NAME>. All Rights Reserved.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0.
 */

#include <time.h>

#include "rvi.h"
#include "jansson.h"

#define BUFSIZE 256
#define LEN 10

void callbackFunc(int fd, void *service_data, json_t *parameters);

void waitFor(unsigned int secs);

void processChoice( int choice );
 
void listChoices(void); 

void initialize(void);

void connect(void);

void disconnect(void);

void get_connections(void);

void register_service(void);

void unregister_service(void);

void get_services(void);

void invoke(void);

void process(void);

void shutdown(void);

rvi_handle myHandle = {0};

int main(int argc, char *argv[])
{
    int choice;

    listChoices();

    while ( scanf("%d", &choice) ) {
        processChoice( choice );
        listChoices();
    }

    return 0;
}

void callbackFunc(int fd, void *service_data, json_t *parameters)
{
    printf("inside the callback function\n");
}

void waitFor(unsigned int secs) 
{
    unsigned int retTime = time(0) + secs;  /* Get end time */
    while( time(0) < retTime);              /* Loop until it arrives */
}

void processChoice( int choice ) 
{
    switch( choice ) {
        case 0:
            printf("You chose to initialize RVI.\n");
            initialize();
            break;
        case 1:
            printf("You chose to connect to a remote node.\n");
            int stat = rvi_connect(myHandle, "192.168.18.76", "9007");
            printf("stat after connect is %d\n", stat);
            //connect_to_node();
            break;
        case 2:
            printf("Which RVI node would you like to disconnect from?\n");
            disconnect();
            break;
        case 3:
            printf("You chose to get a list of connections.\n");
            get_connections();
            break;
        case 4:
            printf("You chose to register a service.\n");
            register_service();
            break;
        case 5:
            printf("You chose to unregister a service.\n");
            unregister_service();
            break;
        case 6:
            printf("You chose to get a list of services.\n");
            get_services();
            break;
        case 7:
            printf("What service would you like to invoke?\n");
            invoke();
            break;
        case 8:
            printf("You chose to process input.\n");
            process();
            break;
        case 9:
            printf("You chose to shutdown RVI.\n");
            shutdown();
            break;
        case 10:
            printf("Goodbye!\n");
            exit(0);
        default:
            printf("That's not a valid choice.\n");
            break;
    }
}

void listChoices(void) {
    printf("Make a choice:\n"
            "\t[0]: Initialize RVI\n"
            "\t[1]: Connect to remote node\n"
            "\t[2]: Disconnect from remote node\n"
            "\t[3]: Get a list of connections\n"
            "\t[4]: Register a service\n"
            "\t[5]: Unregister a service\n"
            "\t[6]: Get a list of services\n"
            "\t[7]: Invoke remote service\n"
            "\t[8]: Process input\n"
            "\t[9]: Shutdown RVI\n"
            "\t[10]: Close program.\n");
}

void initialize(void)
{
    char input[BUFSIZE] = {0};
    printf("Config file: ");
    scanf("%s", input);
    myHandle = rvi_init(input);
    if( !myHandle ) exit(1);
    printf("RVI initialized!\n");
}

void connect_to_node(void)
{
    int stat = rvi_connect(myHandle, "192.168.18.76", "9007");
    if( stat > 0 ) {
        printf("Connected to remote node on fd %d!\n", stat);
    } else {
        printf("Failed to connect to remote node.\n");
    }
    printf("stat after connect is %d\n", stat);
}

void disconnect(void)
{
    int fd;
    scanf("%d", &fd);
    int stat = rvi_disconnect(myHandle, fd);
    printf("stat after disconnecting file descriptor %d is %d\n", fd, stat);
}

void get_connections(void)
{
    int *connections = malloc( LEN * sizeof(int *) );
    int len = LEN;
    int *conn = connections;
    
    for(int i = 0; i < len; i++) {
        connections[i] = 0;
    }

    int stat = rvi_get_connections(myHandle, connections, &len);

    printf("stat after get connections is %d\n", stat);
{'fraction_non_alphanumeric': 0.13188666516752867, 'fraction_numerical': 0.016865302451090622, 'mean_word_length': 2.027569775357386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4064738', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2680, 'n_words': 855}