Example #1
0
int main (void)
{
  TutorialCalculatorHandler *handler;
  CalculatorProcessor *processor;

  ThriftServerTransport *server_transport;
  ThriftTransportFactory *transport_factory;
  ThriftProtocolFactory *protocol_factory;

  struct sigaction sigint_action;

  GError *error;
  int exit_status = 0;

#if (!GLIB_CHECK_VERSION (2, 36, 0))
  g_type_init ();
#endif

  /* Create an instance of our handler, which provides the service's
     methods' implementation */
  handler =
    g_object_new (TYPE_TUTORIAL_CALCULATOR_HANDLER,
                  NULL);

  /* Create an instance of the service's processor, automatically
     generated by the Thrift compiler, which parses incoming messages
     and dispatches them to the appropriate method in the handler */
  processor =
    g_object_new (TYPE_CALCULATOR_PROCESSOR,
                  "handler", handler,
                  NULL);

  /* Create our server socket, which binds to the specified port and
     listens for client connections */
  server_transport =
    g_object_new (THRIFT_TYPE_SERVER_SOCKET,
                  "port", 9090,
                  NULL);

  /* Create our transport factory, used by the server to wrap "raw"
     incoming connections from the client (in this case with a
     ThriftBufferedTransport to improve performance) */
  transport_factory =
    g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT_FACTORY,
                  NULL);

  /* Create our protocol factory, which determines which wire protocol
     the server will use (in this case, Thrift's binary protocol) */
  protocol_factory =
    g_object_new (THRIFT_TYPE_BINARY_PROTOCOL_FACTORY,
                  NULL);

  /* Create the server itself */
  server =
    g_object_new (THRIFT_TYPE_SIMPLE_SERVER,
                  "processor",                processor,
                  "server_transport",         server_transport,
                  "input_transport_factory",  transport_factory,
                  "output_transport_factory", transport_factory,
                  "input_protocol_factory",   protocol_factory,
                  "output_protocol_factory",  protocol_factory,
                  NULL);

  /* Install our SIGINT handler, which handles Ctrl-C being pressed by
     stopping the server gracefully (not strictly necessary, but a
     nice touch) */
  memset (&sigint_action, 0, sizeof (sigint_action));
  sigint_action.sa_handler = sigint_handler;
  sigint_action.sa_flags = SA_RESETHAND;
  sigaction (SIGINT, &sigint_action, NULL);

  /* Start the server, which will run until its stop method is invoked
     (from within the SIGINT handler, in this case) */
  puts ("Starting the server...");
  thrift_server_serve (server, &error);

  /* If the server stopped for any reason other than having been
     interrupted by the user, report the error */
  if (!sigint_received) {
    g_message ("thrift_server_serve: %s",
               error != NULL ? error->message : "(null)");
    g_clear_error (&error);
  }

  puts ("done.");

  g_object_unref (server);
  g_object_unref (transport_factory);
  g_object_unref (protocol_factory);
  g_object_unref (server_transport);

  g_object_unref (processor);
  g_object_unref (handler);

  return exit_status;
}
Example #2
0
int
main (int argc, char **argv)
{
  static gint   port = 9090;
  static gchar *server_type_option = NULL;
  static gchar *transport_option = NULL;
  static gchar *protocol_option = NULL;
  static gint   string_limit = 0;
  static gint   container_limit = 0;

  static
    GOptionEntry option_entries[] = {
    { "port",            0, 0, G_OPTION_ARG_INT,      &port,
      "Port number to connect (=9090)", NULL },
    { "server-type",     0, 0, G_OPTION_ARG_STRING,   &server_type_option,
      "Type of server: simple (=simple)", NULL },
    { "transport",       0, 0, G_OPTION_ARG_STRING,   &transport_option,
      "Transport: buffered, framed (=buffered)", NULL },
    { "protocol",        0, 0, G_OPTION_ARG_STRING,   &protocol_option,
      "Protocol: binary, compact (=binary)", NULL },
    { "string-limit",    0, 0, G_OPTION_ARG_INT,      &string_limit,
      "Max string length (=none)", NULL },
    { "container-limit", 0, 0, G_OPTION_ARG_INT,      &container_limit,
      "Max container length (=none)", NULL },
    { NULL }
  };

  gchar *server_name            = "simple";
  gchar *transport_name         = "buffered";
  GType  transport_factory_type = THRIFT_TYPE_BUFFERED_TRANSPORT_FACTORY;
  gchar *protocol_name          = "binary";
  GType  protocol_factory_type  = THRIFT_TYPE_BINARY_PROTOCOL_FACTORY;

  TTestThriftTestHandler *handler;
  ThriftProcessor        *processor;
  ThriftServerTransport  *server_transport;
  ThriftTransportFactory *transport_factory;
  ThriftProtocolFactory  *protocol_factory;

  struct sigaction sigint_action;

  GOptionContext *option_context;
  gboolean        options_valid = TRUE;

  GError *error = NULL;

#if (!GLIB_CHECK_VERSION (2, 36, 0))
  g_type_init ();
#endif

  /* Configure and parse our command-line options */
  option_context = g_option_context_new (NULL);
  g_option_context_add_main_entries (option_context,
                                     option_entries,
                                     NULL);
  if (g_option_context_parse (option_context,
                              &argc,
                              &argv,
                              &error) == FALSE) {
    fprintf (stderr, "%s\n", error->message);
    return 255;
  }
  g_option_context_free (option_context);

  /* Validate the parsed options */
  if (server_type_option != NULL &&
      strncmp (server_type_option, "simple", 7) != 0) {
    fprintf (stderr, "Unknown server type %s\n", protocol_option);
    options_valid = FALSE;
  }

  if (protocol_option != NULL) {
    if (strncmp (protocol_option, "compact", 8) == 0) {
      protocol_factory_type = THRIFT_TYPE_COMPACT_PROTOCOL_FACTORY;
      protocol_name = "compact";
    }
    else if (strncmp (protocol_option, "binary", 7) != 0) {
      fprintf (stderr, "Unknown protocol type %s\n", protocol_option);
      options_valid = FALSE;
    }
  }

  if (transport_option != NULL) {
    if (strncmp (transport_option, "framed", 7) == 0) {
      transport_factory_type = THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY;
      transport_name = "framed";
    }
    else if (strncmp (transport_option, "buffered", 9) != 0) {
      fprintf (stderr, "Unknown transport type %s\n", transport_option);
      options_valid = FALSE;
    }
  }

  if (!options_valid)
    return 254;

  /* Establish all our connection objects */
  handler           = g_object_new (TYPE_THRIFT_TEST_HANDLER,
                                    NULL);
  processor         = g_object_new (T_TEST_TYPE_THRIFT_TEST_PROCESSOR,
                                    "handler", handler,
                                    NULL);
  server_transport  = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
                                    "port", port,
                                    NULL);
  transport_factory = g_object_new (transport_factory_type,
                                    NULL);

  if (strncmp (protocol_name, "compact", 8) == 0) {
    protocol_factory  = g_object_new (protocol_factory_type,
                                      "string_limit", string_limit,
                                      "container_limit", container_limit,
                                      NULL);
  } else {
    protocol_factory  = g_object_new (protocol_factory_type,
                                      NULL);
  }

  server = g_object_new (THRIFT_TYPE_SIMPLE_SERVER,
                         "processor",                processor,
                         "server_transport",         server_transport,
                         "input_transport_factory",  transport_factory,
                         "output_transport_factory", transport_factory,
                         "input_protocol_factory",   protocol_factory,
                         "output_protocol_factory",  protocol_factory,
                         NULL);

  /* Install our SIGINT handler, which handles Ctrl-C being pressed by stopping
     the server gracefully */
  memset (&sigint_action, 0, sizeof (sigint_action));
  sigint_action.sa_handler = sigint_handler;
  sigint_action.sa_flags = SA_RESETHAND;
  sigaction (SIGINT, &sigint_action, NULL);

  printf ("Starting \"%s\" server (%s/%s) listen on: %d\n",
          server_name,
          transport_name,
          protocol_name,
          port);
  fflush (stdout);

  /* Serve clients until SIGINT is received (Ctrl-C is pressed) */
  thrift_server_serve (server, &error);

  /* If the server stopped for any reason other than being interrupted by the
     user, report the error */
  if (!sigint_received) {
    g_message ("thrift_server_serve: %s",
               error != NULL ? error->message : "(null)");
    g_clear_error (&error);
  }

  puts ("done.");

  g_object_unref (server);
  g_object_unref (protocol_factory);
  g_object_unref (transport_factory);
  g_object_unref (server_transport);
  g_object_unref (processor);
  g_object_unref (handler);

  return 0;
}