Back to home page

Enduro/X

 
 

    


0001 /**
0002  *
0003  * @file atmisv.c
0004  */
0005 /* -----------------------------------------------------------------------------
0006  * Enduro/X Middleware Platform for Distributed Transaction Processing
0007  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0008  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0009  * This software is released under one of the following licenses:
0010  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0011  * See LICENSE file for full text.
0012  * -----------------------------------------------------------------------------
0013  * AGPL license:
0014  *
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU Affero General Public License, version 3 as published
0017  * by the Free Software Foundation;
0018  *
0019  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0020  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0021  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0022  * for more details.
0023  *
0024  * You should have received a copy of the GNU Affero General Public License along 
0025  * with this program; if not, write to the Free Software Foundation, Inc.,
0026  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0027  *
0028  * -----------------------------------------------------------------------------
0029  * A commercial use license is available from Mavimax, Ltd
0030  * contact@mavimax.com
0031  * -----------------------------------------------------------------------------
0032  */
0033 
0034 /* Stuff for sockets testing: */
0035 #include <ndrx_config.h>
0036 #include <sys/socket.h>
0037 #include <sys/time.h>
0038 #include <sys/types.h>
0039 #include <arpa/inet.h>
0040 #include <netinet/in.h>
0041 #include <errno.h>
0042 #include <fcntl.h>
0043 #include <netdb.h>
0044 #include <string.h>
0045 #include <unistd.h>
0046 
0047 #include <ndrstandard.h>
0048 
0049 #if defined(EX_USE_EPOLL)
0050 
0051 #include <sys/epoll.h>
0052 
0053 #elif defined(EX_USE_KQUEUE)
0054 
0055 #include <sys/event.h>
0056 
0057 #else
0058 
0059 #include <poll.h>
0060 
0061 #endif
0062 
0063 #include <stdio.h>
0064 #include <stdlib.h>
0065 #include <ndebug.h>
0066 #include <atmi.h>
0067 #include <ubf.h>
0068 #include <test.fd.h>
0069 
0070 
0071 #if defined(EX_USE_EPOLL)
0072 
0073 #define POLL_FLAGS (EPOLLIN | EPOLLHUP)
0074 
0075 #elif defined(EX_USE_KQUEUE)
0076 
0077 #define POLL_FLAGS (EVFILT_READ)
0078 
0079 #else
0080 
0081 #define POLL_FLAGS (POLLIN)
0082 
0083 #endif
0084 
0085 void TESTSVFN (TPSVCINFO *p_svc);
0086 
0087 char *test_ptr = "THIS IS TEST";
0088 
0089 /*
0090  * This is test service!
0091  */
0092 void TESTSVFN (TPSVCINFO *p_svc)
0093 {
0094     int ret=EXSUCCEED;
0095 
0096     char *str = "THIS IS TEST - OK!";
0097 
0098     UBFH *p_ub = (UBFH *)p_svc->data;
0099 
0100     NDRX_LOG(log_debug, "TESTSVFN got call");
0101 
0102     /* Just print the buffer */
0103     Bprint(p_ub);
0104     if (NULL==(p_ub = (UBFH *)tprealloc((char *)p_ub, 4096))) /* allocate some stuff for more data to put in  */
0105     {
0106         ret=EXFAIL;
0107         goto out;
0108     }
0109 
0110     if (EXFAIL==Bchg(p_ub, T_STRING_FLD, 0, (char *)str, 0))
0111     {
0112         ret=EXFAIL;
0113         goto out;
0114     }
0115 
0116 out:
0117     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0118                 0L,
0119                 (char *)p_ub,
0120                 0L,
0121                 0L);
0122 }
0123 
0124 /**
0125  * Socket extension
0126  * @param fd
0127  * @param events
0128  * @return 
0129  */
0130 int poll_connect_22(int fd, uint32_t events, void *ptr1)
0131 {
0132     int ret=EXSUCCEED;    
0133     int so_error=0;
0134     int sz;
0135     char buf[2048];
0136     socklen_t len = sizeof so_error;
0137 
0138     getsockopt(fd, SOL_SOCKET, SO_ERROR, &so_error, &len);
0139     
0140     NDRX_LOG(log_warn, "POLL1EXT_OK, events: %d", events);
0141     
0142     if (so_error == 0) 
0143     { 
0144         NDRX_LOG(log_debug,"%s:%s is open\n", "127.0.0.1", "22");
0145         /* Read some stuff!! */
0146         if ((sz=recv(fd, buf, 2048, 0))>0)
0147         {
0148             NDRX_DUMP(log_error, "Got stuff from socket: ", buf, sz);
0149         }
0150         else if (sz==0)
0151         {
0152             NDRX_LOG(log_debug,"peer disconnected");
0153             sleep(1);
0154         }
0155         else
0156         {
0157             NDRX_LOG(log_debug,"error: %s", strerror(errno));
0158         }
0159     }
0160     else
0161     {
0162         NDRX_LOG(log_debug,"so_error=%d - %s\n", so_error, strerror(so_error));
0163     }
0164     
0165     if (NULL==ptr1 || 0!=strcmp((char *)ptr1, "THIS IS TEST"))
0166     {
0167         NDRX_LOG(log_debug,"TESTERROR: ptr1 does not work in poll ext!");
0168     }
0169     
0170     
0171 out:
0172     return ret;
0173 }
0174 
0175 /**
0176  * Before poll function
0177  * @return 
0178  */
0179 int b4poll_cb(void)
0180 {
0181     NDRX_LOG(log_warn, "B4POLL_CALLED");
0182     return EXSUCCEED;
0183 }
0184 /**
0185  * Periodical callback function
0186  * @param argc
0187  * @param argv
0188  * @return 
0189  */
0190 int periodical_cb(void)
0191 {
0192     static int first=EXTRUE;
0193     NDRX_LOG(log_debug, "PERIODCB_OK");
0194     
0195     if (first)
0196     {
0197         /* Example from: http://stackoverflow.com/questions/2597608/c-socket-connection-timeout */
0198         u_short port;                /* user specified port number */
0199         char *addr;                  /* will be a pointer to the address */
0200         struct sockaddr_in address;  /* the libc network address data structure */
0201         short int sock = -1;         /* file descriptor for the network socket */
0202 
0203         NDRX_LOG(log_debug, "Try to connect somewhere, and "
0204                         "check will we get Event!");
0205 
0206         port = 22;
0207         addr = "127.0.0.1";
0208 
0209         address.sin_family = AF_INET;
0210         address.sin_addr.s_addr = inet_addr(addr); /* assign the address */
0211         address.sin_port = htons(port);            /* translate int2port num */
0212 
0213         sock = socket(AF_INET, SOCK_STREAM, 0);
0214         fcntl(sock, F_SETFL, O_NONBLOCK);
0215 
0216         connect(sock, (struct sockaddr *)&address, sizeof(address));
0217         
0218         if (EXSUCCEED!=tpext_addpollerfd(sock, 
0219                 POLL_FLAGS,
0220                 (void *)test_ptr, poll_connect_22))
0221         {
0222             NDRX_LOG(log_error, "TESTERROR: tpext_addpollerfd failed!");
0223         }
0224         
0225         first=EXFALSE;
0226         
0227     }
0228     
0229     
0230     return EXSUCCEED;
0231 }
0232 
0233 /*
0234  * Do initialization
0235  */
0236 int NDRX_INTEGRA(tpsvrinit)(int argc, char **argv)
0237 {
0238     NDRX_LOG(log_debug, "tpsvrinit called");
0239 
0240 
0241     /* Step 1: add periodical call */
0242     if (EXSUCCEED!=tpext_addperiodcb(1, periodical_cb))
0243     {
0244         NDRX_LOG(log_error, "TESTERROR: tpext_addperiodcb failed!");
0245     }
0246     
0247     if (EXSUCCEED!=tpext_addb4pollcb(b4poll_cb))
0248     {
0249         NDRX_LOG(log_error, "TESTERROR: tpext_addb4pollcb failed!");
0250     }
0251     
0252     return EXSUCCEED;
0253 }
0254 
0255 /**
0256  * Do de-initialization
0257  */
0258 void NDRX_INTEGRA(tpsvrdone)(void)
0259 {
0260     NDRX_LOG(log_debug, "tpsvrdone called");
0261 }
0262 /* vim: set ts=4 sw=4 et smartindent: */