Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glib-object.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <thrift/c_glib/thrift.h>
- #include <thrift/c_glib/protocol/thrift_binary_protocol_factory.h>
- #include <thrift/c_glib/protocol/thrift_protocol_factory.h>
- #include <thrift/c_glib/server/thrift_server.h>
- #include <thrift/c_glib/server/thrift_simple_server.h>
- #include <thrift/c_glib/transport/thrift_buffered_transport_factory.h>
- #include <thrift/c_glib/transport/thrift_server_socket.h>
- #include <thrift/c_glib/transport/thrift_server_transport.h>
- #include "gen-c_glib/calculator.h"
- G_BEGIN_DECLS
- #define TYPE_TUTORIAL_CALCULATOR_HANDLER \
- (tutorial_calculator_handler_get_type ())
- #define TUTORIAL_CALCULATOR_HANDLER(obj) \
- (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
- TYPE_TUTORIAL_CALCULATOR_HANDLER, \
- TutorialCalculatorHandler))
- #define TUTORIAL_CALCULATOR_HANDLER_CLASS(c) \
- (G_TYPE_CHECK_CLASS_CAST ((c), \
- TYPE_TUTORIAL_CALCULATOR_HANDLER, \
- TutorialCalculatorHandlerClass))
- #define IS_TUTORIAL_CALCULATOR_HANDLER(obj) \
- (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
- TYPE_TUTORIAL_CALCULATOR_HANDLER))
- #define IS_TUTORIAL_CALCULATOR_HANDLER_CLASS(c) \
- (G_TYPE_CHECK_CLASS_TYPE ((c), \
- TYPE_TUTORIAL_CALCULATOR_HANDLER))
- #define TUTORIAL_CALCULATOR_HANDLER_GET_CLASS(obj) \
- (G_TYPE_INSTANCE_GET_CLASS ((obj), \
- TYPE_TUTORIAL_CALCULATOR_HANDLER, \
- TutorialCalculatorHandlerClass))
- struct _TutorialCalculatorHandler {
- CalculatorHandler parent_instance;
- /* private */
- GHashTable *log;
- };
- typedef struct _TutorialCalculatorHandler TutorialCalculatorHandler;
- struct _TutorialCalculatorHandlerClass {
- CalculatorHandlerClass parent_class;
- };
- typedef struct _TutorialCalculatorHandlerClass TutorialCalculatorHandlerClass;
- GType tutorial_calculator_handler_get_type (void);
- G_END_DECLS
- /* ---------------------------------------------------------------- */
- /* The implementation of TutorialCalculatorHandler follows. */
- G_DEFINE_TYPE (TutorialCalculatorHandler,
- tutorial_calculator_handler,
- TYPE_CALCULATOR_HANDLER)
- static gboolean
- tutorial_calculator_handler_ping (CalculatorIf *iface,
- GError **error)
- {
- THRIFT_UNUSED_VAR (iface);
- THRIFT_UNUSED_VAR (error);
- puts ("ping()");
- return TRUE;
- }
- /* TutorialCalculatorHandler's instance finalizer (destructor) */
- static void
- tutorial_calculator_handler_finalize (GObject *object)
- {
- TutorialCalculatorHandler *self =
- TUTORIAL_CALCULATOR_HANDLER (object);
- /* Free our calculation-log hash table */
- g_hash_table_unref (self->log);
- self->log = NULL;
- /* Chain up to the parent class */
- G_OBJECT_CLASS (tutorial_calculator_handler_parent_class)->
- finalize (object);
- }
- /* TutorialCalculatorHandler's instance initializer (constructor) */
- static void
- tutorial_calculator_handler_init (TutorialCalculatorHandler *self)
- {
- /* Create our calculation-log hash table */
- self->log = g_hash_table_new_full (g_int_hash,
- g_int_equal,
- g_free,
- g_object_unref);
- }
- /* TutorialCalculatorHandler's class initializer */
- static void
- tutorial_calculator_handler_class_init (TutorialCalculatorHandlerClass *klass)
- {
- GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
- CalculatorHandlerClass *calculator_handler_class =
- CALCULATOR_HANDLER_CLASS (klass);
- /* Register our destructor */
- gobject_class->finalize = tutorial_calculator_handler_finalize;
- /* Register our implementations of CalculatorHandler's methods */
- calculator_handler_class->ping =
- tutorial_calculator_handler_ping;
- /*calculator_handler_class->add =
- tutorial_calculator_handler_add;
- calculator_handler_class->calculate =
- tutorial_calculator_handler_calculate;
- calculator_handler_class->zip =
- tutorial_calculator_handler_zip;
- */
- /* Register our implementation of SharedServiceHandler's method */
- /*shared_service_handler_class->get_struct =
- tutorial_calculator_handler_get_struct;*/
- }
- /* ---------------------------------------------------------------- */
- /* That ends the implementation of TutorialCalculatorHandler.
- Everything below is fairly generic code that sets up a minimal
- Thrift server for tutorial clients. */
- /* Our server object, declared globally so it is accessible within the
- SIGINT signal handler */
- ThriftServer *server = NULL;
- /* A flag that indicates whether the server was interrupted with
- SIGINT (i.e. Ctrl-C) so we can tell whether its termination was
- abnormal */
- gboolean sigint_received = FALSE;
- /* Handle SIGINT ("Ctrl-C") signals by gracefully stopping the
- server */
- static void
- sigint_handler (int signal_number)
- {
- THRIFT_UNUSED_VAR (signal_number);
- /* Take note we were called */
- sigint_received = TRUE;
- /* Shut down the server gracefully */
- if (server != NULL)
- thrift_server_stop (server);
- }
- int main (void)
- {
- TutorialCalculatorHandler *handler;
- CalculatorProcessor *processor;
- ThriftServerTransport *server_transport;
- ThriftTransportFactory *transport_factory;
- ThriftProtocolFactory *protocol_factory;
- GError *error = NULL;
- 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) */
- /* 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 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement