examples/test-http-proxy.c
changeset 664 f57b1b61e1fe
parent 518 cdd6a0c5b439
child 690 7ccf2113ec5f
equal deleted inserted replaced
663:3697251ef911 664:f57b1b61e1fe
    15  * You should have received a copy of the GNU Lesser General Public
    15  * You should have received a copy of the GNU Lesser General Public
    16  * License along with this program; if not, write to the
    16  * License along with this program; if not, write to the
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    18  * Boston, MA 02111-1307, USA.
    18  * Boston, MA 02111-1307, USA.
    19  */
    19  */
    20  
    20 
    21 #include <config.h>
    21 #include <config.h>
    22  
    22 
    23 #include <glib.h>
    23 #include <glib.h>
    24 #include <string.h>
    24 #include <string.h>
    25 #include <stdlib.h>
    25 #include <stdlib.h>
    26 #include <loudmouth/loudmouth.h>
    26 #include <loudmouth/loudmouth.h>
    27 #ifdef G_OS_WIN32
    27 #ifdef G_OS_WIN32
    28 #include <winsock2.h>
    28 #include <winsock2.h>
    29 #endif
    29 #endif
    30  
    30 
    31 typedef struct {
    31 typedef struct {
    32     gchar *name;
    32     gchar *name;
    33     gchar *passwd;
    33     gchar *passwd;
    34 } UserInfo;
    34 } UserInfo;
    35  
    35 
    36 static void
    36 static void
    37 free_user_info (UserInfo *info)
    37 free_user_info (UserInfo *info)
    38 {
    38 {
    39     g_free (info->name);
    39     g_free (info->name);
    40     g_free (info->passwd);
    40     g_free (info->passwd);
    41  
    41 
    42     g_free (info);
    42     g_free (info);
    43 }
    43 }
    44  
    44 
    45  
    45 
    46 static void
    46 static void
    47 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
    47 authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
    48 {
    48 {
    49     g_print ("Auth: %d\n", result);
    49     g_print ("Auth: %d\n", result);
    50     free_user_info ((UserInfo *) ud);
    50     free_user_info ((UserInfo *) ud);
    51  
    51 
    52     if (result == TRUE) {
    52     if (result == TRUE) {
    53         LmMessage *m;
    53         LmMessage *m;
    54                  
    54 
    55         m = lm_message_new_with_sub_type (NULL,
    55         m = lm_message_new_with_sub_type (NULL,
    56                                           LM_MESSAGE_TYPE_PRESENCE,
    56                                           LM_MESSAGE_TYPE_PRESENCE,
    57                                           LM_MESSAGE_SUB_TYPE_AVAILABLE);
    57                                           LM_MESSAGE_SUB_TYPE_AVAILABLE);
    58         g_print (":: %s\n", lm_message_node_to_string (m->node));
    58         g_print (":: %s\n", lm_message_node_to_string (m->node));
    59                  
    59 
    60         lm_connection_send (connection, m, NULL);
    60         lm_connection_send (connection, m, NULL);
    61         lm_message_unref (m);
    61         lm_message_unref (m);
    62     }
    62     }
    63 
    63 
    64 }
    64 }
    65  
    65 
    66 static void
    66 static void
    67 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
    67 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
    68 {
    68 {
    69     g_print ("Connected callback\n");
    69     g_print ("Connected callback\n");
    70     lm_connection_authenticate (connection,
    70     lm_connection_authenticate (connection,
    71                                 info->name, info->passwd, "TestLM",
    71                                 info->name, info->passwd, "TestLM",
    72                                 authentication_cb, info, FALSE,  NULL);
    72                                 authentication_cb, info, FALSE,  NULL);
    73     g_print ("Sent auth message\n");
    73     g_print ("Sent auth message\n");
    74 }
    74 }
    75  
    75 
    76 static LmHandlerResult
    76 static LmHandlerResult
    77 handle_messages (LmMessageHandler *handler,
    77 handle_messages (LmMessageHandler *handler,
    78                  LmConnection     *connection,
    78                  LmConnection     *connection,
    79                  LmMessage        *m,
    79                  LmMessage        *m,
    80                  gpointer          user_data)
    80                  gpointer          user_data)
    81 {
    81 {
    82     g_print ("Incoming message from: %s\n",
    82     g_print ("Incoming message from: %s\n",
    83              lm_message_node_get_attribute (m->node, "from"));
    83              lm_message_node_get_attribute (m->node, "from"));
    84  
    84 
    85     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
    85     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
    86 }
    86 }
    87 int
    87 int
    88 main (int argc, char **argv)
    88 main (int argc, char **argv)
    89 {
    89 {
    90     GMainLoop        *main_loop;
    90     GMainLoop        *main_loop;
    91     LmConnection     *connection;
    91     LmConnection     *connection;
    92     LmMessageHandler *handler;
    92     LmMessageHandler *handler;
    93     gboolean          result;
    93     gboolean          result;
    94     UserInfo         *info;
    94     UserInfo         *info;
    95     LmProxy          *proxy; 
    95     LmProxy          *proxy;
    96     guint             proxy_port;
    96     guint             proxy_port;
    97                                                                                 
    97 
    98     if (argc < 6) {
    98     if (argc < 6) {
    99         g_print ("Usage: %s <server> <username> <password> <proxyserver> <proxyport>\n", argv[0]);
    99         g_print ("Usage: %s <server> <username> <password> <proxyserver> <proxyport>\n", argv[0]);
   100         return 1;
   100         return 1;
   101     }
   101     }
   102                                                                                 
   102 
   103     connection = lm_connection_new (argv[1]);
   103     connection = lm_connection_new (argv[1]);
   104 
   104 
   105     proxy = lm_proxy_new (LM_PROXY_TYPE_HTTP);
   105     proxy = lm_proxy_new (LM_PROXY_TYPE_HTTP);
   106     lm_proxy_set_server (proxy, argv[4]);
   106     lm_proxy_set_server (proxy, argv[4]);
   107 
   107 
   108     proxy_port = strtol (argv[5], (char **) NULL, 10);
   108     proxy_port = strtol (argv[5], (char **) NULL, 10);
   109     lm_proxy_set_port (proxy, proxy_port);
   109     lm_proxy_set_port (proxy, proxy_port);
   110     lm_connection_set_proxy (connection, proxy);
   110     lm_connection_set_proxy (connection, proxy);
   111     lm_proxy_unref (proxy);
   111     lm_proxy_unref (proxy);
   112                                                                                 
   112 
   113     handler = lm_message_handler_new (handle_messages, NULL, NULL);
   113     handler = lm_message_handler_new (handle_messages, NULL, NULL);
   114     lm_connection_register_message_handler (connection, handler,
   114     lm_connection_register_message_handler (connection, handler,
   115                                             LM_MESSAGE_TYPE_MESSAGE,
   115                                             LM_MESSAGE_TYPE_MESSAGE,
   116                                             LM_HANDLER_PRIORITY_NORMAL);
   116                                             LM_HANDLER_PRIORITY_NORMAL);
   117                                                                                 
   117 
   118     lm_message_handler_unref (handler);
   118     lm_message_handler_unref (handler);
   119                                                                                 
   119 
   120     info = g_new0 (UserInfo, 1);
   120     info = g_new0 (UserInfo, 1);
   121     info->name = g_strdup (argv[2]);
   121     info->name = g_strdup (argv[2]);
   122     info->passwd = g_strdup (argv[3]);
   122     info->passwd = g_strdup (argv[3]);
   123                                                                                 
   123 
   124     result = lm_connection_open (connection,
   124     result = lm_connection_open (connection,
   125                                  (LmResultFunction) connection_open_cb,
   125                                  (LmResultFunction) connection_open_cb,
   126                                  info, NULL, NULL);
   126                                  info, NULL, NULL);
   127                                                                                 
   127 
   128     if (!result) {
   128     if (!result) {
   129         g_print ("Opening connection failed: %d\n", result);
   129         g_print ("Opening connection failed: %d\n", result);
   130     } else {
   130     } else {
   131         g_print ("Returned from the connection_open\n");
   131         g_print ("Returned from the connection_open\n");
   132     }
   132     }
   133                                                                                 
   133 
   134     main_loop = g_main_loop_new (NULL, FALSE);
   134     main_loop = g_main_loop_new (NULL, FALSE);
   135     g_main_loop_run (main_loop);
   135     g_main_loop_run (main_loop);
   136                                                                                 
   136 
   137     return 0;
   137     return 0;
   138 }
   138 }
   139 
   139