Advertisement
Guest User

thrift-server.c

a guest
Sep 18th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.29 KB | None | 0 0
  1.  
  2.     #include <glib-object.h>
  3.     #include <signal.h>
  4.     #include <stdio.h>
  5.     #include <stdlib.h>
  6.     #include <string.h>
  7.  
  8.     #include <thrift/c_glib/thrift.h>
  9.     #include <thrift/c_glib/protocol/thrift_binary_protocol_factory.h>
  10.     #include <thrift/c_glib/protocol/thrift_protocol_factory.h>
  11.     #include <thrift/c_glib/server/thrift_server.h>
  12.     #include <thrift/c_glib/server/thrift_simple_server.h>
  13.     #include <thrift/c_glib/transport/thrift_buffered_transport_factory.h>
  14.     #include <thrift/c_glib/transport/thrift_server_socket.h>
  15.     #include <thrift/c_glib/transport/thrift_server_transport.h>
  16.  
  17.     #include "gen-c_glib/calculator.h"
  18.  
  19.     G_BEGIN_DECLS
  20.  
  21.  
  22.     #define TYPE_TUTORIAL_CALCULATOR_HANDLER \
  23.       (tutorial_calculator_handler_get_type ())
  24.  
  25.     #define TUTORIAL_CALCULATOR_HANDLER(obj)                                \
  26.       (G_TYPE_CHECK_INSTANCE_CAST ((obj),                                   \
  27.                                    TYPE_TUTORIAL_CALCULATOR_HANDLER,        \
  28.                                    TutorialCalculatorHandler))
  29.     #define TUTORIAL_CALCULATOR_HANDLER_CLASS(c)                    \
  30.       (G_TYPE_CHECK_CLASS_CAST ((c),                                \
  31.                                 TYPE_TUTORIAL_CALCULATOR_HANDLER,   \
  32.                                 TutorialCalculatorHandlerClass))
  33.     #define IS_TUTORIAL_CALCULATOR_HANDLER(obj)                             \
  34.       (G_TYPE_CHECK_INSTANCE_TYPE ((obj),                                   \
  35.                                    TYPE_TUTORIAL_CALCULATOR_HANDLER))
  36.     #define IS_TUTORIAL_CALCULATOR_HANDLER_CLASS(c)                 \
  37.       (G_TYPE_CHECK_CLASS_TYPE ((c),                                \
  38.                                 TYPE_TUTORIAL_CALCULATOR_HANDLER))
  39.     #define TUTORIAL_CALCULATOR_HANDLER_GET_CLASS(obj)              \
  40.       (G_TYPE_INSTANCE_GET_CLASS ((obj),                            \
  41.                                   TYPE_TUTORIAL_CALCULATOR_HANDLER, \
  42.                                   TutorialCalculatorHandlerClass))
  43.  
  44.     struct _TutorialCalculatorHandler {
  45.       CalculatorHandler parent_instance;
  46.  
  47.       /* private */
  48.       GHashTable *log;
  49.     };
  50.     typedef struct _TutorialCalculatorHandler TutorialCalculatorHandler;
  51.  
  52.     struct _TutorialCalculatorHandlerClass {
  53.       CalculatorHandlerClass parent_class;
  54.     };
  55.     typedef struct _TutorialCalculatorHandlerClass TutorialCalculatorHandlerClass;
  56.  
  57.     GType tutorial_calculator_handler_get_type (void);
  58.  
  59.     G_END_DECLS
  60.  
  61.     /* ---------------------------------------------------------------- */
  62.  
  63.     /* The implementation of TutorialCalculatorHandler follows. */
  64.  
  65.     G_DEFINE_TYPE (TutorialCalculatorHandler,
  66.                    tutorial_calculator_handler,
  67.                    TYPE_CALCULATOR_HANDLER)
  68.  
  69.     static gboolean
  70.     tutorial_calculator_handler_ping (CalculatorIf  *iface,
  71.                                       GError       **error)
  72.     {
  73.       THRIFT_UNUSED_VAR (iface);
  74.       THRIFT_UNUSED_VAR (error);
  75.  
  76.       puts ("ping()");
  77.  
  78.       return TRUE;
  79.     }
  80.  
  81.     /* TutorialCalculatorHandler's instance finalizer (destructor) */
  82.     static void
  83.     tutorial_calculator_handler_finalize (GObject *object)
  84.     {
  85.       TutorialCalculatorHandler *self =
  86.         TUTORIAL_CALCULATOR_HANDLER (object);
  87.  
  88.       /* Free our calculation-log hash table */
  89.       g_hash_table_unref (self->log);
  90.       self->log = NULL;
  91.  
  92.       /* Chain up to the parent class */
  93.       G_OBJECT_CLASS (tutorial_calculator_handler_parent_class)->
  94.         finalize (object);
  95.     }
  96.  
  97.     /* TutorialCalculatorHandler's instance initializer (constructor) */
  98.     static void
  99.     tutorial_calculator_handler_init (TutorialCalculatorHandler *self)
  100.     {
  101.       /* Create our calculation-log hash table */
  102.       self->log = g_hash_table_new_full (g_int_hash,
  103.                                          g_int_equal,
  104.                                          g_free,
  105.                                          g_object_unref);
  106.     }
  107.  
  108.     /* TutorialCalculatorHandler's class initializer */
  109.     static void
  110.     tutorial_calculator_handler_class_init (TutorialCalculatorHandlerClass *klass)
  111.     {
  112.       GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  113.      
  114.       CalculatorHandlerClass *calculator_handler_class =
  115.         CALCULATOR_HANDLER_CLASS (klass);
  116.  
  117.       /* Register our destructor */
  118.       gobject_class->finalize = tutorial_calculator_handler_finalize;
  119.  
  120.       /* Register our implementations of CalculatorHandler's methods */
  121.       calculator_handler_class->ping =
  122.         tutorial_calculator_handler_ping;
  123.       /*calculator_handler_class->add =
  124.         tutorial_calculator_handler_add;
  125.       calculator_handler_class->calculate =
  126.         tutorial_calculator_handler_calculate;
  127.       calculator_handler_class->zip =
  128.         tutorial_calculator_handler_zip;
  129.     */
  130.       /* Register our implementation of SharedServiceHandler's method */
  131.       /*shared_service_handler_class->get_struct =
  132.         tutorial_calculator_handler_get_struct;*/
  133.     }
  134.  
  135.     /* ---------------------------------------------------------------- */
  136.  
  137.     /* That ends the implementation of TutorialCalculatorHandler.
  138.        Everything below is fairly generic code that sets up a minimal
  139.        Thrift server for tutorial clients. */
  140.  
  141.  
  142.     /* Our server object, declared globally so it is accessible within the
  143.        SIGINT signal handler */
  144.     ThriftServer *server = NULL;
  145.  
  146.     /* A flag that indicates whether the server was interrupted with
  147.        SIGINT (i.e. Ctrl-C) so we can tell whether its termination was
  148.        abnormal */
  149.     gboolean sigint_received = FALSE;
  150.  
  151.     /* Handle SIGINT ("Ctrl-C") signals by gracefully stopping the
  152.        server */
  153.     static void
  154.     sigint_handler (int signal_number)
  155.     {
  156.       THRIFT_UNUSED_VAR (signal_number);
  157.  
  158.       /* Take note we were called */
  159.       sigint_received = TRUE;
  160.  
  161.       /* Shut down the server gracefully */
  162.       if (server != NULL)
  163.         thrift_server_stop (server);
  164.     }
  165.  
  166.     int main (void)
  167.     {
  168.       TutorialCalculatorHandler *handler;
  169.       CalculatorProcessor *processor;
  170.  
  171.       ThriftServerTransport *server_transport;
  172.       ThriftTransportFactory *transport_factory;
  173.       ThriftProtocolFactory *protocol_factory;
  174.  
  175.       GError *error = NULL;
  176.       int exit_status = 0;
  177.  
  178.     #if (!GLIB_CHECK_VERSION (2, 36, 0))
  179.       g_type_init ();
  180.     #endif
  181.  
  182.       /* Create an instance of our handler, which provides the service's
  183.          methods' implementation */
  184.       handler =
  185.         g_object_new (TYPE_TUTORIAL_CALCULATOR_HANDLER,
  186.                       NULL);
  187.  
  188.       /* Create an instance of the service's processor, automatically
  189.          generated by the Thrift compiler, which parses incoming messages
  190.          and dispatches them to the appropriate method in the handler */
  191.       processor =
  192.         g_object_new (TYPE_CALCULATOR_PROCESSOR,
  193.                       "handler", handler,
  194.                       NULL);
  195.  
  196.       /* Create our server socket, which binds to the specified port and
  197.          listens for client connections */
  198.       server_transport =
  199.         g_object_new (THRIFT_TYPE_SERVER_SOCKET,
  200.                       "port", 9090,
  201.                       NULL);
  202.  
  203.       /* Create our transport factory, used by the server to wrap "raw"
  204.          incoming connections from the client (in this case with a
  205.          ThriftBufferedTransport to improve performance) */
  206.       transport_factory =
  207.         g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT_FACTORY,
  208.                       NULL);
  209.  
  210.       /* Create our protocol factory, which determines which wire protocol
  211.          the server will use (in this case, Thrift's binary protocol) */
  212.       protocol_factory =
  213.         g_object_new (THRIFT_TYPE_BINARY_PROTOCOL_FACTORY,
  214.                       NULL);
  215.  
  216.       /* Create the server itself */
  217.       server =
  218.         g_object_new (THRIFT_TYPE_SIMPLE_SERVER,
  219.                       "processor",                processor,
  220.                       "server_transport",         server_transport,
  221.                       "input_transport_factory",  transport_factory,
  222.                       "output_transport_factory", transport_factory,
  223.                       "input_protocol_factory",   protocol_factory,
  224.                       "output_protocol_factory",  protocol_factory,
  225.                       NULL);
  226.  
  227.       /* Install our SIGINT handler, which handles Ctrl-C being pressed by
  228.          stopping the server gracefully (not strictly necessary, but a
  229.          nice touch) */
  230.  
  231.       /* Start the server, which will run until its stop method is invoked
  232.          (from within the SIGINT handler, in this case) */
  233.       puts ("Starting the server...");
  234.       thrift_server_serve (server, &error);
  235.  
  236.       /* If the server stopped for any reason other than having been
  237.          interrupted by the user, report the error */
  238.       if (!sigint_received) {
  239.         g_message ("thrift_server_serve: %s",
  240.                    error != NULL ? error->message : "(null)");
  241.         g_clear_error (&error);
  242.       }
  243.  
  244.       puts ("done.");
  245.  
  246.     /*  g_object_unref (server);
  247.       g_object_unref (transport_factory);
  248.       g_object_unref (protocol_factory);
  249.       g_object_unref (server_transport);
  250.  
  251.       g_object_unref (processor);
  252.       g_object_unref (handler);*/
  253.  
  254.       return 1;
  255.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement