MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cdrserver.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 /* **************************************************************** */
20 /* */
21 /* S E R V . T C P */
22 /* * This is an example program that demonstrates the use of */
23 /* stream sockets as an IPC mechanism. This contains the server, */
24 /* and is intended to operate in conjunction with the client */
25 /* program found in client.tcp. Together, these two programs */
26 /* demonstrate many of the features of sockets, as well as good */
27 /* conventions for using these features. */
28 /* * This program provides a service called "example". In order for*/
29 /* it to function, an entry for it needs to exist in the */
30 /* ./etc/services file. The port address for this service can be */
31 /* any port number that is likely to be unused, such as 22375, */
32 /* for example. The host on which the client will be running */
33 /* must also have the same entry (same port number) in its */
34 /* ./etc/services file. */
35 /* **************************************************************** */
36 
37 #include <ndb_global.h>
38 
39 /******** NDB INCLUDE ******/
40 #include <NdbApi.hpp>
41 /***************************/
42 /*#include <sys/shm.h>*/
43 #include <pthread.h>
44 #include <sys/sem.h>
45 #include <sys/shm.h>
46 #include <netinet/in.h>
47 #include <signal.h>
48 #include <netdb.h>
49 #include <time.h>
50 #include <synch.h>
51 #include <sched.h>
52 
53 extern "C" {
54 #include "utv.h"
55 #include "vcdrfunc.h"
56 #include "bcd.h"
57 }
58 
59 #ifndef TESTLEV
60 #define TESTLEV
61 #endif
62 //#define DEBUG
63 //#define MYDEBUG
64 //#define SETDBG
65 
66 //#define ops_before_exe 64
67 #define MAXOPSEXEC 1024
68 
69 /* Used in nanosleep */
70 /**** NDB ********/
71 static int bTestPassed;
72 void create_table(Ndb* pMyNdb);
73 void error_handler(const char* errorText);
74 /*****************/
75 static struct timespec tmspec1;
76 static int server(long int);
77 
78 /* Function for initiating the cdr-area and make it clean for ongoing calls */
79 
80 static int s; /* connected socket descriptor */
81 static int ls; /* listen socket descriptor */
82 
83 static struct hostent *hp; /* pointer to host info for remote host */
84 static struct servent *sp; /* pointer to service information */
85 
86 struct linger linger; /* allow a lingering, graceful close; */
87  /* used when setting SO_LINGER */
88 
89 static struct sockaddr_in myaddr_in; /* for local socket address */
90 static struct sockaddr_in peeraddr_in; /* for peer socket address */
91 
92 static FILE *fi; /* Log output */
93 static char temp[600]="";
94 
95 static int ops_before_exe = 1; /* Number of operations per execute, default is 1,
96  but it can be changed with the -o parameter. */
97 
98 /*----------------------------------------------------------------------
99 
100  M A I N
101  * This routine starts the server. It forks, leaving the child
102  to do all the work, so it does not have to be run in the
103  background. It sets up the listen socket, and for each incoming
104  connection, it forks a child process to process the data. It
105  will loop forever, until killed by a signal.
106 
107  ----------------------------------------------------------------------*/
108 
109 /****** NDB *******/
110 static char *tableName = "VWTABLE";
111 /******************/
112 
113 #include <iostream>
114 using namespace std;
115 
116 int main(int argc, const char** argv)
117 {
118  ndb_init();
119  /******** NDB ***********/
120  /*
121  Ndb MyNdb( "TEST_DB" );
122  int tTableId;
123  */
124  /************************/
125  char tmpbuf[400];
126  /* Loop and status variables */
127  int i,j,found;
128 
129  /* Used by the server */
130  int addrlen;
131 
132  /* return code used with functions */
133  int rc;
134 
135  i = 1;
136  while (argc > 1)
137  {
138  if (strcmp(argv[i], "-o") == 0)
139  {
140  ops_before_exe = atoi(argv[i+1]);
141  if ((ops_before_exe < 1) || (ops_before_exe > MAXOPSEXEC))
142  {
143  cout << "Number of operations per execute must be at least 1, and at most " << MAXOPSEXEC << endl;
144  exit(1);
145  }
146 
147  }
148  else
149  {
150  cout << "Invalid parameter!" << endl << "Look in cdrserver.C for more info." << endl;
151  exit(1);
152  }
153 
154  argc -= 2;
155  i = i + 2;
156  }
157 
158 
159  /* Setup log handling */
160  logname(temp,"Cdrserver","Mother","");
161  puts(temp);
162  fi=fopen(temp,"w");
163  if (fi == NULL)
164  {
165  perror(argv[0]);
166  exit(EXIT_FAILURE);
167  }
168  m2log(fi,"Initiation of program");
169 
170  /***** NDB ******/
171  /*
172  MyNdb.init();
173  if (MyNdb.waitUntilReady(30) != 0)
174  {
175  puts("Not ready");
176  exit(-1);
177  }
178  tTableId = MyNdb.getTable()->openTable(tableName);
179  if (tTableId == -1)
180  {
181  printf("%d: Creating table",getpid());
182  create_table(&MyNdb);
183  }
184  else printf("%d: Table already create",getpid());
185  */
186 
187  /****************/
188 
189  /* clear out address structures */
190  memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
191  memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
192 
193  m2log(fi,"Socket setup starting");
194 
195  /* Set up address structure for the listen socket. */
196  myaddr_in.sin_family = AF_INET;
197 
198  /* The server should listen on the wildcard address, */
199  /* rather than its own internet address. This is */
200  /* generally good practice for servers, because on */
201  /* systems which are connected to more than one */
202  /* network at once will be able to have one server */
203  /* listening on all networks at once. Even when the */
204  /* host is connected to only one network, this is good */
205  /* practice, because it makes the server program more */
206  /* portable. */
207 
208  myaddr_in.sin_addr.s_addr = INADDR_ANY;
209  /* Find the information for the "cdrserver" server */
210  /* in order to get the needed port number. */
211 
212  sp = getservbyname ("cdrserver", "tcp");
213  if (sp == NULL) {
214  m2log(fi,"Service cdrserver not found in /etc/services");
215  m2log(fi,"Terminating.");
216  exit(EXIT_FAILURE);
217  }
218 
219  myaddr_in.sin_port = sp->s_port;
220 
221  /* Create the listen socket.i */
222 
223  ls = socket (AF_INET, SOCK_STREAM, 0);
224  if (ls == -1) {
225  m2log(fi,"Unable to create socket");
226  m2log(fi,"Terminating.");
227  exit(EXIT_FAILURE);
228  }
229  printf("Socket created\n");
230  printf("Wait..........\n");
231  /* Bind the listen address to the socket. */
232  if (bind(ls,(struct sockaddr*)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
233  m2log(fi,"Unable to bind address");
234  m2log(fi,"Terminating.");
235  exit(EXIT_FAILURE);
236  }
237 
238  /* Initiate the listen on the socket so remote users */
239  /* can connect. The listen backlog is set to 5, which */
240  /* is the largest currently supported. */
241 
242  if (listen(ls, 5) == -1) {
243  m2log(fi,"Unable to listen on socket");
244  m2log(fi,"Terminating.");
245  exit(EXIT_FAILURE);
246  }
247 
248  /* Now, all the initialization of the server is */
249  /* complete, and any user errors will have already */
250  /* been detected. Now we can fork the daemon and */
251  /* return to the user. We need to do a setpgrp */
252  /* so that the daemon will no longer be associated */
253  /* with the user's control terminal. This is done */
254  /* before the fork, so that the child will not be */
255  /* a process group leader. Otherwise, if the child */
256  /* were to open a terminal, it would become associated */
257  /* with that terminal as its control terminal. It is */
258  /* always best for the parent to do the setpgrp. */
259 
260  m2log(fi,"Socket setup completed");
261  m2log(fi,"Start server");
262 
263  setpgrp();
264 
265  /* Initiate the tmspec struct for use with nanosleep() */
266  tmspec1.tv_sec = 0;
267  tmspec1.tv_nsec = 1;
268 
269  printf("Waiting for client to connect.........\n");
270  printf("Done\n");
271  switch (fork()) {
272  case -1: /* Unable to fork, for some reason. */
273  m2log(fi,"Failed to start server");
274  m2log(fi,"Terminating.");
275  fclose(fi);
276  perror(argv[0]);
277  fprintf(stderr, "%s: unable to fork daemon\n", argv[0]);
278  exit(EXIT_FAILURE);
279 
280  break;
281  case 0: /* The child process (daemon) comes here. */
282  m2log(fi,"Server started");
283 
284  /* Close stdin and stderr so that they will not */
285  /* be kept open. Stdout is assumed to have been */
286  /* redirected to some logging file, or /dev/null. */
287  /* From now on, the daemon will not report any */
288  /* error messages. This daemon will loop forever, */
289  /* waiting for connections and forking a child */
290  /* server to handle each one. */
291 
292  close((int)stdin);
293  close((int)stderr);
294  /* Set SIGCLD to SIG_IGN, in order to prevent */
295  /* the accumulation of zombies as each child */
296  /* terminates. This means the daemon does not */
297  /* have to make wait calls to clean them up. */
298 
299  signal(SIGCLD, SIG_IGN);
300  for(EVER) {
301  if ((checkchangelog(fi,temp))==0)
302  m2log(fi,"Waiting for connection");
303  /* Note that addrlen is passed as a pointer */
304  /* so that the accept call can return the */
305  /* size of the returned address. */
306 
307  addrlen = sizeof(struct sockaddr_in);
308 
309  /* This call will block until a new */
310  /* connection arrives. Then, it will */
311  /* return the address of the connecting */
312  /* peer, and a new socket descriptor, s, */
313  /* for that connection. */
314 
315  s = accept(ls,(struct sockaddr*) &peeraddr_in, &addrlen);
316  #ifdef MYDEBUG
317  puts("accepted");
318  #endif
319  if ((checkchangelog(fi,temp))==0)
320  m2log(fi,"Connection attempt from a client");
321  if ((checkchangelog(fi,temp))==0)
322  m2log(fi,"Start communication server");
323 
324  if ( s == -1) exit(EXIT_FAILURE);
325  switch (fork()) {
326  case -1: /* Can't fork, just exit. */
327  if ((checkchangelog(fi,temp))==0)
328  m2log(fi,"Start communication server failed.");
329  exit(EXIT_FAILURE);
330  break;
331  case 0: /* Child process comes here. */
332 
333  /* Get clients adress and save it in the info area */
334  /* Keep track of how many times the client connects to the server */
335  printf("Connect attempt from client %u\n",peeraddr_in.sin_addr.s_addr);
336  server(peeraddr_in.sin_addr.s_addr);
337  exit(EXIT_FAILURE);
338  break;
339  default: /* Daemon process comes here. */
340  /* The daemon needs to remember */
341  /* to close the new accept socket */
342  /* after forking the child. This */
343  /* prevents the daemon from running */
344  /* out of file descriptor space. It */
345  /* also means that when the server */
346  /* closes the socket, that it will */
347  /* allow the socket to be destroyed */
348  /* since it will be the last close. */
349  close(s);
350  break;
351  }
352  }
353  default: /* Parent process comes here. */
354  exit(EXIT_FAILURE);
355  }
356  return EXIT_SUCCESS;
357 }
358 
359 /*----------------------------------------------------------------------
360 
361  S E R V E R
362  * This is the actual server routine that the daemon forks to
363  handle each individual connection. Its purpose is to receive
364  the request packets from the remote client, process them,
365  and return the results to the client. It will also write some
366  logging information to stdout.
367 
368  ----------------------------------------------------------------------*/
369 
370 server(long int servernum)
371 {
372  /******** NDB ***********/
373  Ndb MyNdb( "TEST_DB" );
374  int tTableId;
375  NdbConnection *MyTransaction;
376  NdbOperation *MyOperation;
377  int check;
378  int c1 = 0;
379  int c2 = 0;
380  int c3 = 0;
381  int c4 = 0;
382  int act_index = 0;
383  /************************/
384  register unsigned int reqcnt; /* keeps count of number of requests */
385  register unsigned int i; /* Loop counters */
386  register int x;
387  register short done; /* Loop variable */
388  short int found;
389 
390  /* The server index number */
391  int thisServer;
392 
393  /* Variables used to keep track of some statistics */
394  time_t ourtime;
395  time_t tmptime;
396  int tmpvalue;
397  long int tmptransfer;
398  long int transfer;
399  int ops = 0;
400 
401  /* Variables used by the server */
402  char buf[400]; /* This example uses 10 byte messages. */
403  char *inet_ntoa();
404  char *hostname; /* points to the remote host's name string */
405  int len;
406  int rcvbuf_size;
407 
408  long ctid;
409 
410  unsigned char uc;
411 
412  /* Variables used by the logging facilitiy */
413  char msg[600];
414  char crap[600];
415  char lognamn[600];
416 
417  FILE *log;
418 
419  /* scheduling parameter for pthread */
420  struct sched_param param1,param2,param3;
421 
422  /* Header information */
423  /* cdrtype not used */
424  /*short cdrtype; */ /* 1 CDR Typ */
425  short cdrlen; /* 2 CDR recored length in bytes excluding CDR type */
426  short cdrsubtype; /* 1 CDR subtype */
427  unsigned int cdrid; /* 8 CDR unique number of each call */
428  unsigned int cdrtime; /* 4 CDR Time in seconds */
429  short cdrmillisec; /* 2 CDR Milliseconds */
430  short cdrstatus; /* 1 CDR For future use */
431  short cdrequipeid; /* 1 CDR Equipment id */
432  int cdrreserved1; /* 4 CDR For future use */
433 
434  /* Defined or calculated for each record */
435  int cdrrestlen; /* Unprocessed data left in record in bytes */
436 
437  /* Gemensamma datatyper */
438  unsigned short parmtype_prev; /* 1 Parameter type */
439  unsigned short parmtype; /* 1 Parameter type */
440  unsigned short parmlen; /* 1 Parameter type */
441 
442  int rc; /* return code for functions */
443 
444  /* Attribute object used with threads */
445  pthread_attr_t attr1;
446  pthread_attr_t attr2;
447  pthread_attr_t attr3;
448  struct cdr_record *tmpcdrptr,*ftest;
449  void *dat;
450 
451  int error_from_client = 0;
452 
453  /* Konstanter */
454  const int headerlen = 24; /* Length of header record */
455 
456  parmtype_prev = 99;
457  reqcnt = 0;
458 
459  /* Close the listen socket inherited from the daemon. */
460  close(ls);
461 
462  printf("Use the readinfo program to get information about server status\n\n");
463 
464  if((checkchangelog(fi,temp))==0)
465  c2log(fi,"Communication server started");
466 
467  /* Look up the host information for the remote host */
468  /* that we have connected with. Its internet address */
469  /* was returned by the accept call, in the main */
470  /* daemon loop above. */
471 
472  hp=gethostbyaddr((char *) &peeraddr_in.sin_addr,sizeof(struct in_addr),peeraddr_in.sin_family);
473 
474  if (hp == NULL) {
475  /* The information is unavailable for the remote */
476  /* host. Just format its internet address to be */
477  /* printed out in the logging information. The */
478  /* address will be shown in "internet dot format". */
479 
480  /*
481  hostname = inet_ntoa(peeraddr_in.sin_addr);
482  */
483  sprintf(hostname,"Test");
484  logname(lognamn,"Cdrserver","Child",hostname);
485  }
486  else {
487  hostname = hp->h_name; /* point to host's name */
488  logname(lognamn,"Cdrserver","Child",hostname);
489  }
490 
491  log=fopen(lognamn,"w");
492  if (log == NULL)
493  {
494  perror(hostname);
495  exit(EXIT_FAILURE);
496  }
497  n2log(log,"Setup in progress");
498  /* Log a startup message. */
499 
500  /* The port number must be converted first to host byte */
501  /* order before printing. On most hosts, this is not */
502  /* necessary, but the ntohs() call is included here so */
503  /* that this program could easily be ported to a host */
504  /* that does require it. */
505 
506  BaseString::snprintf(msg,sizeof(msg),"Startup from %s port %u",hostname,ntohs(peeraddr_in.sin_port));
507  if ((checkchangelog(fi,temp))==0)
508  c2log(fi,msg);
509  n2log(log,msg);
510  BaseString::snprintf(msg,sizeof(msg),"For further information, see log(%s)",lognamn);
511  if ((checkchangelog(fi,temp))==0)
512  c2log(fi,msg);
513 
514  /* Set the socket for a lingering, graceful close. */
515  /* This will cause a final close of this socket to wait until */
516  /* all * data sent on it has been received by the remote host. */
517 
518  linger.l_onoff =1;
519  linger.l_linger =0;
520  if (setsockopt(s, SOL_SOCKET, SO_LINGER,(const char*)&linger,sizeof(linger)) == -1) {
521  BaseString::snprintf(msg,sizeof(msg),"Setting SO_LINGER, l_onoff=%d, l_linger=%d",linger.l_onoff,linger.l_linger);
522  if ((checkchangelog(log,lognamn))==0)
523  n2log(log,msg);
524  goto errout;
525  }
526 
527  /* Set the socket for a lingering, graceful close. */
528  /* This will cause a final close of this socket to wait until all * data sent */
529  /* on it has been received by the remote host. */
530 
531  rcvbuf_size=64*1024;
532 
533  if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,(const char*) &rcvbuf_size,sizeof(rcvbuf_size)) == -1) {
534  BaseString::snprintf(msg,sizeof(msg),"Setting SO_RCVBUF = %d",rcvbuf_size);
535  if ((checkchangelog(log,lognamn))==0)
536  n2log(log,msg);
537  goto errout;
538  }
539 
540  /* Set nodelay on socket */
541  n2log(log,"Port setup complete");
542 
543  /* Go into a loop, receiving requests from the remote */
544  /* client. After the client has sent the last request, */
545  /* it will do a shutdown for sending, which will cause */
546  /* an end-of-file condition to appear on this end of the */
547  /* connection. After all of the client's requests have */
548  /* been received, the next recv call will return zero */
549  /* bytes, signalling an end-of-file condition. This is */
550  /* how the server will know that no more requests will */
551  /* follow, and the loop will be exited. */
552 
553  n2log(log,"Setup completed");
554 
555  /* Fetch the process id for the server */
556 
557  /* Inititate the variables used for counting transfer rates and rec/sec */
558  tmpvalue = 0;
559  tmptime = 0;
560  tmptransfer = 0;
561  transfer = 0;
562 
563  printf("Client %s connected\nStarting to process the data\n\n",hostname);
564 
565  tmpcdrptr = (struct cdr_record*)malloc(sizeof(struct cdr_record));
566 
567  /***** NDB ******/
568  MyNdb.init();
569  if (MyNdb.waitUntilReady(30) != 0)
570  {
571  puts("Not ready");
572  exit(-1);
573  }
574  tTableId = MyNdb.getTable()->openTable(tableName);
575  if (tTableId == -1)
576  {
577  printf("%d: Creating table",getpid());
578  create_table(&MyNdb);
579  }
580  else printf("%d: Table already created",getpid());
581 
582  /****************/
583 
584  while (len = recv(s,buf,headerlen,MSG_WAITALL)) {
585  if (len == -1) {
586  snprintf(msg,sizeof(msg),"Error from recv");
587  if ((checkchangelog(log,lognamn))==0)
588  n2log(log,msg);
589  goto errout; /* error from recv */
590  }
591 
592  /* The reason this while loop exists is that there */
593  /* is a remote possibility of the above recv returning */
594  /* less than 10 bytes. This is because a recv returns */
595  /* as soon as there is some data, and will not wait for */
596  /* all of the requested data to arrive. Since 10 bytes */
597  /* is relatively small compared to the allowed TCP */
598  /* packet sizes, a partial receive is unlikely. If */
599  /* this example had used 2048 bytes requests instead, */
600  /* a partial receive would be far more likely. */
601  /* This loop will keep receiving until all 10 bytes */
602  /* have been received, thus guaranteeing that the */
603  /* next recv at the top of the loop will start at */
604  /* the begining of the next request. */
605 
606  for (;len < headerlen;) {
607  x = recv(s,buf,(headerlen-len),0);
608  if (x == -1) {
609  snprintf(msg,sizeof(msg),"Error from recv");
610  if ((checkchangelog(log,lognamn))==0)
611  n2log(log,msg);
612  goto errout; /* error from recv */
613  }
614  len=len+x;
615  }
616 
617  if (ops == 0) {
618  MyTransaction = MyNdb.startTransaction();
619  if (MyTransaction == NULL)
620  error_handler(MyNdb.getNdbErrorString());
621  }//if
622 
623  MyOperation = MyTransaction->getNdbOperation(tableName);
624  if (MyOperation == NULL)
625  error_handler(MyTransaction->getNdbErrorString());
626  /*------------------------------------------------------*/
627  /* Parse header of CDR records */
628  /*------------------------------------------------------*/
629 
630  /*------------------------------------------------------*/
631  /* 1. Type of cdr */
632  /*------------------------------------------------------*/
633  /* Not used for the moment
634  cdrtype=(char)buf[0];
635  */
636  /*------------------------------------------------------*/
637  /* 2. Total length of CDR */
638  /*------------------------------------------------------*/
639  swab(buf+1,buf+1,2);
640  memcpy(&cdrlen,buf+1,2);
641  /*------------------------------------------------------*/
642  /* 3. Partial type of CDR */
643  /*------------------------------------------------------*/
644  cdrsubtype=(char)buf[3];
645  switch (cdrsubtype)
646  {
647  case 0:
648  c1++;
649  tmpcdrptr->CallAttemptState = 1;
650  check = MyOperation->insertTuple();
651  break;
652  case 1:
653  c2++;
654  tmpcdrptr->CallAttemptState = 2;
655  check = MyOperation->updateTuple();
656  break;
657  case 2:
658  c3++;
659  tmpcdrptr->CallAttemptState = 3;
660  check = MyOperation->deleteTuple();
661  break;
662  case 3:
663  c4++;
664  tmpcdrptr->CallAttemptState = 4;
665  check = MyOperation->deleteTuple();
666  break;
667  if (check == -1)
668  error_handler(MyTransaction->getNdbErrorString());
669  }
670  /*cdrsubtype=(cdrsubtype << 24) >> 24;*/
671  /*------------------------------------------------------*/
672  /* 4. ID number */
673  /*------------------------------------------------------*/
674  /*swab(buf+4,buf+4,4);*/ /* ABCD -> BADC */
675  /*
676  swab(buf+4,buf+4,4);
677  swab(buf+5,buf+5,2);
678  swab(buf+6,buf+6,2);
679  swab(buf+4,buf+4,2);
680  swab(buf+5,buf+5,2);
681  */
682  memcpy(&cdrid,buf+4,4);
683  tmpcdrptr->CallIdentificationNumber = cdrid;
684  #ifdef SETDBG
685  puts("CIN");
686  #endif
687  check = MyOperation->equal("CIN",(char*)&cdrid);
688  if (check == -1)
689  error_handler(MyTransaction->getNdbErrorString());
690  #ifdef SETDBG
691  puts("CAS");
692  #endif
693 
694  if (cdrsubtype < 2)
695  {
696  check = MyOperation->setValue("CAS",(char*)&cdrsubtype);
697  if (check == -1)
698  error_handler(MyTransaction->getNdbErrorString());
699  }
700  /*------------------------------------------------------*/
701  /* 5. Time stamp */
702  /*------------------------------------------------------*/
703  swab(buf+12,buf+12,4);
704  swab(buf+13,buf+13,2);
705  swab(buf+14,buf+14,2);
706  swab(buf+12,buf+12,2);
707  swab(buf+13,buf+13,2);
708  memcpy(&cdrtime,buf+12,4);
709  switch (cdrsubtype)
710  {
711  case 0:
712  #ifdef SETDBG
713  puts("START_TIME");
714  #endif
715  check = MyOperation->setValue("START_TIME",(char*)&cdrtime);
716  break;
717  case 1:
718  #ifdef SETDBG
719  puts("Start1");
720  #endif
721  check = MyOperation->setValue("StartOfCharge",(char*)&cdrtime);
722  break;
723  case 2:
724  #ifdef SETDBG
725  puts("Start2");
726  #endif
727  /*
728  check = MyOperation->setValue("StopOfCharge",(char*)&cdrtime);
729  */
730  check = 0;
731  break;
732  if (check == -1)
733  error_handler(MyTransaction->getNdbErrorString());
734  }
735  /*------------------------------------------------------*/
736  /* 6. Milliseconds */
737  /*------------------------------------------------------*/
738  /* Not used by application
739  swab(buf+16,buf+16,2);
740  memcpy(&cdrmillisec,buf+16,2);
741  */
742  /*------------------------------------------------------*/
743  /* 7. CDR status reserverd for future use */
744  /*------------------------------------------------------*/
745  /* Not used by application
746  memcpy(&cdrstatus,buf+18,1);
747  */
748  /*------------------------------------------------------*/
749  /* 8. CDR equipe id, number of sending equipement */
750  /*------------------------------------------------------*/
751  /* Not used by application
752  memcpy(&cdrequipeid,buf+19,1);
753  */
754  /*cdrequipeid=(cdrequipeid << 24) >> 24;*/
755  /*------------------------------------------------------*/
756  /* 9. CDR reserverd for furter use */
757  /*------------------------------------------------------*/
758  /* Not used by applikation
759  swab(buf+20,buf+20,4);
760  swab(buf+21,buf+21,2);
761  swab(buf+22,buf+22,2);
762  swab(buf+20,buf+20,2);
763  swab(buf+21,buf+21,2);
764  memcpy(&cdrreserved1,buf+20,4);
765  */
766  /*------------------------------------------------------*/
767  /* calculate length of datapart in record */
768  /* Formula recordlength-headerlen-1 */
769  /*------------------------------------------------------*/
770  cdrrestlen=cdrlen-(headerlen-1);
771  /*------------------------------------------------------*/
772  /* Finished with header */
773  /*------------------------------------------------------*/
774  /* Read remaining cdr data into buffer for furter */
775  /* handling. */
776  /*------------------------------------------------------*/
777  len = recv(s,buf,cdrrestlen,MSG_WAITALL);
778  if (len == -1) {
779  snprintf(msg,sizeof(msg),"Error from recv");
780  if ((checkchangelog(log,lognamn))==0)
781  n2log(log,msg);
782  goto errout; /* error from recv */
783  }
784  for (;len<cdrrestlen;) {
785  x = recv(s,buf,len-cdrrestlen,0);
786  if (x == -1) {
787  snprintf(msg,sizeof(msg),"Error from recv");
788  if ((checkchangelog(log,lognamn))==0)
789  n2log(log,msg);
790  goto errout; /* error from recv */
791  }
792  len=len+x;
793  }
794  done=FALSE;
795 
796  /* Count the transfer/sec */
797  tmptransfer += cdrlen;
798  if (cdrsubtype > 1)
799  {
800  #ifdef SETDBG
801  puts("Going to execute");
802  #endif
803  ops++;
804  if (ops == ops_before_exe) {
805  ops = 0;
806  check = MyTransaction->execute(Commit, CommitAsMuchAsPossible);
807  if ((check == -1) && (MyTransaction->getNdbError() != 0))
808  error_handler(MyTransaction->getNdbErrorString());
809  MyNdb.closeTransaction(MyTransaction);
810  #ifdef SETDBG
811  puts("Transaction closed");
812  #endif
813  }//if
814  reqcnt++;
815  continue;
816  }
817  for (x=0;x<=cdrrestlen && !done && cdrrestlen > 1;) {
818  uc=buf[x];
819  parmtype=uc;
820  /*parmtype=(parmtype << 24) >> 24;*/ /* Modified in sun worked in hp */
821 
822  parmlen = buf[x+1];
823  /*parmlen =(parmlen << 24) >> 24;*/
824  x+=2;
825 
826  switch (parmtype) {
827  case 4: /* Called party number */
828  bcd_decode2(parmlen,&buf[x],crap);
829  tmpcdrptr->BSubscriberNumberLength = (char)parmlen;
830  strcpy(tmpcdrptr->BSubscriberNumber,crap);
831  tmpcdrptr->BSubscriberNumber[parmlen] = '\0';
832  x=x+(parmlen/2);
833  if (parmlen % 2) x++;
834  tmpcdrptr->USED_FIELDS |= B_BSubscriberNumber;
835  #ifdef SETDBG
836  puts("BNumber");
837  #endif
838  check = MyOperation->setValue("BNumber",(char*)&tmpcdrptr->BSubscriberNumber);
839  if (check == -1)
840  error_handler(MyTransaction->getNdbErrorString());
841  break;
842  case 9: /* Calling Partys cataegory */
843  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
844  else tmpcdrptr->ACategory=(char)buf[x];
845  x+=parmlen;
846  tmpcdrptr->USED_FIELDS |= B_ACategory;
847  #ifdef SETDBG
848  puts("ACategory");
849  #endif
850  check = MyOperation->setValue("ACategory",(char*)&tmpcdrptr->ACategory);
851  if (check == -1)
852  error_handler(MyTransaction->getNdbErrorString());
853  break;
854  case 10: /* Calling Party Number */
855  bcd_decode2(parmlen,&buf[x],crap);
856  tmpcdrptr->ASubscriberNumberLength = (char)parmlen;
857  strcpy(tmpcdrptr->ASubscriberNumber,crap);
858  tmpcdrptr->ASubscriberNumber[parmlen] = '\0';
859  x=x+(parmlen/2);
860  if (parmlen % 2) x++;
861  tmpcdrptr->USED_FIELDS |= B_ASubscriberNumber;
862  #ifdef SETDBG
863  puts("ANumber");
864  #endif
865  check = MyOperation->setValue("ANumber",(char*)&tmpcdrptr->ASubscriberNumber);
866  if (check == -1)
867  error_handler(MyTransaction->getNdbErrorString());
868  break;
869  case 11: /* Redirecting number */
870  bcd_decode2(parmlen,&buf[x],crap);
871  strcpy(tmpcdrptr->RedirectingNumber,crap);
872  x=x+(parmlen/2);
873  if (parmlen % 2) x++;
874  tmpcdrptr->USED_FIELDS |= B_RedirectingNumber;
875  #ifdef SETDBG
876  puts("RNumber");
877  #endif
878  check = MyOperation->setValue("RNumber",(char*)&tmpcdrptr->RedirectingNumber);
879  if (check == -1)
880  error_handler(MyTransaction->getNdbErrorString());
881  break;
882  case 17: /* Called partys category */
883  if (parmlen != 1) printf("ERROR: Called partys category has wrong length %d\n",parmlen);
884  else tmpcdrptr->EndOfSelectionInformation=(char)buf[x];
885  x+=parmlen;
886  tmpcdrptr->USED_FIELDS |= B_EndOfSelectionInformation;
887  #ifdef SETDBG
888  puts("EndOfSelInf");
889  #endif
890  check = MyOperation->setValue("EndOfSelInf",(char*)&tmpcdrptr->EndOfSelectionInformation);
891  if (check == -1)
892  error_handler(MyTransaction->getNdbErrorString());
893  break;
894  case 18: /* Release reason */
895  if (parmlen != 1) printf("ERROR: Release reason has wrong length %d\n",parmlen);
896  else tmpcdrptr->CauseCode=(char)buf[x];
897  x+=parmlen;
898  tmpcdrptr->USED_FIELDS |= B_CauseCode;
899  #ifdef SETDBG
900  puts("CauseCode");
901  #endif
902  check = MyOperation->setValue("CauseCode",(char*)&tmpcdrptr->CauseCode);
903  if (check == -1)
904  error_handler(MyTransaction->getNdbErrorString());
905  break;
906  case 19: /* Redirection information */
907  switch (parmlen) {
908  case 1:
909  tmpcdrptr->ReroutingIndicator= (char)buf[x];
910  tmpcdrptr->USED_FIELDS |= B_ReroutingIndicator;
911  break;
912  case 2:
913  swab(buf+x,buf+x,2);
914  tmpcdrptr->ReroutingIndicator= buf[x];
915  tmpcdrptr->USED_FIELDS |= B_ReroutingIndicator;
916  break;
917  default :
918  BaseString::snprintf(msg,sizeof(msg),"ERROR: Redirection information has wrong length %d\n",parmlen);
919  if ((checkchangelog(log,lognamn))==0)
920  n2log(log,msg);
921  break;
922  #ifdef SETDBG
923  puts("RI");
924  #endif
925  check = MyOperation->setValue("RI",(char*)&tmpcdrptr->ReroutingIndicator);
926  if (check == -1)
927  error_handler(MyTransaction->getNdbErrorString());
928  }
929  x+=parmlen;
930  break;
931  case 32: /* User to user information */
932  if (parmlen != 1) printf("ERROR: User to User information has wrong length %d\n",parmlen);
933  else tmpcdrptr->UserToUserInformation=(char)buf[x];
934  x+=parmlen;
935  tmpcdrptr->USED_FIELDS |= B_UserToUserInformation;
936  #ifdef SETDBG
937  puts("UserToUserInf");
938  #endif
939  check = MyOperation->setValue("UserToUserInf",(char*)&tmpcdrptr->UserToUserInformation);
940  if (check == -1)
941  error_handler(MyTransaction->getNdbErrorString());
942  break;
943  case 40: /* Original called number */
944  bcd_decode2(parmlen,&buf[x],crap);
945  strcpy(tmpcdrptr->OriginalCalledNumber,crap);
946  x=x+(parmlen/2);
947  if (parmlen % 2) x++;
948  tmpcdrptr->USED_FIELDS |= B_OriginalCalledNumber;
949  #ifdef SETDBG
950  puts("ONumber");
951  #endif
952  check = MyOperation->setValue("ONumber",(char*)&tmpcdrptr->OriginalCalledNumber);
953  if (check == -1)
954  error_handler(MyTransaction->getNdbErrorString());
955  break;
956  case 42: /* User to user indicator */
957  if (parmlen != 1) printf("ERROR: User to User indicator has wrong length %d\n",parmlen);
958  else tmpcdrptr->UserToUserIndicatior=(char)buf[x];
959  x+=parmlen;
960  tmpcdrptr->USED_FIELDS |= B_UserToUserIndicatior;
961  #ifdef SETDBG
962  puts("UserToUserInd");
963  #endif
964  check = MyOperation->setValue("UserToUserInd",(char*)&tmpcdrptr->UserToUserIndicatior);
965  if (check == -1)
966  error_handler(MyTransaction->getNdbErrorString());
967  break;
968  case 63: /* Location number */
969  bcd_decode2(parmlen,&buf[x],crap);
970  strcpy(tmpcdrptr->LocationCode,crap);
971  x=x+(parmlen/2);
972  if (parmlen % 2) x++;
973  tmpcdrptr->USED_FIELDS |= B_LocationCode;
974  #ifdef SETDBG
975  puts("LocationCode");
976  #endif
977  check = MyOperation->setValue("LocationCode",(char*)&tmpcdrptr->LocationCode);
978  if (check == -1)
979  error_handler(MyTransaction->getNdbErrorString());
980  break;
981  case 240: /* Calling Partys cataegory */
982  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
983  else tmpcdrptr->NetworkIndicator=(char)buf[x];
984  x+=parmlen;
985  tmpcdrptr->USED_FIELDS |= B_NetworkIndicator;
986  #ifdef SETDBG
987  puts("NIndicator");
988  #endif
989  check = MyOperation->setValue("NIndicator",(char*)&tmpcdrptr->NetworkIndicator);
990  if (check == -1)
991  error_handler(MyTransaction->getNdbErrorString());
992  break;
993  case 241: /* Calling Partys cataegory */
994  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
995  else tmpcdrptr->TonASubscriberNumber=(char)buf[x];
996  x+=parmlen;
997  tmpcdrptr->USED_FIELDS |= B_TonASubscriberNumber;
998  #ifdef SETDBG
999  puts("TonANumber");
1000  #endif
1001  check = MyOperation->setValue("TonANumber",(char*)&tmpcdrptr->TonASubscriberNumber);
1002  if (check == -1)
1003  error_handler(MyTransaction->getNdbErrorString());
1004  break;
1005  case 242: /* Calling Partys cataegory */
1006  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
1007  else tmpcdrptr->TonBSubscriberNumber=(char)buf[x];
1008  x+=parmlen;
1009  tmpcdrptr->USED_FIELDS |= B_TonBSubscriberNumber;
1010  #ifdef SETDBG
1011  puts("TonBNumber");
1012  #endif
1013  check = MyOperation->setValue("TonBNumber",(char*)&tmpcdrptr->TonBSubscriberNumber);
1014  if (check == -1)
1015  error_handler(MyTransaction->getNdbErrorString());
1016  break;
1017  case 243: /* Calling Partys cataegory */
1018  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
1019  else tmpcdrptr->TonRedirectingNumber=(char)buf[x];
1020  x+=parmlen;
1021  tmpcdrptr->USED_FIELDS |= B_TonRedirectingNumber;
1022  #ifdef SETDBG
1023  puts("TonRNumber");
1024  #endif
1025  check = MyOperation->setValue("TonRNumber",(char*)&tmpcdrptr->TonRedirectingNumber);
1026  if (check == -1)
1027  error_handler(MyTransaction->getNdbErrorString());
1028  break;
1029  case 244: /* Calling Partys cataegory */
1030  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
1031  else tmpcdrptr->TonOriginalCalledNumber=(char)buf[x];
1032  x+=parmlen;
1033  tmpcdrptr->USED_FIELDS |= B_TonOriginalCalledNumber;
1034  #ifdef SETDBG
1035  puts("TonONumber");
1036  #endif
1037  check = MyOperation->setValue("TonONumber",(char*)&tmpcdrptr->TonOriginalCalledNumber);
1038  if (check == -1)
1039  error_handler(MyTransaction->getNdbErrorString());
1040  break;
1041  case 245: /* Calling Partys cataegory */
1042  if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
1043  else tmpcdrptr->TonLocationCode=(char)buf[x];
1044  x+=parmlen;
1045  tmpcdrptr->USED_FIELDS |= B_TonLocationCode;
1046  #ifdef SETDBG
1047  puts("TonLocationCode");
1048  #endif
1049  check = MyOperation->setValue("TonLocationCode",(char*)&tmpcdrptr->TonLocationCode);
1050  if (check == -1)
1051  error_handler(MyTransaction->getNdbErrorString());
1052  break;
1053  case 252: /* RINParameter Parameter */
1054  switch (parmlen) {
1055  case 1:
1056  tmpcdrptr->RINParameter=buf[x];
1057  tmpcdrptr->USED_FIELDS |= B_RINParameter;
1058  break;
1059  case 2:
1060  swab(buf+x,buf+x,2);
1061  tmpcdrptr->RINParameter = buf[x] << 8;
1062  tmpcdrptr->USED_FIELDS |= B_RINParameter;
1063  break;
1064  default :
1065  BaseString::snprintf(msg,sizeof(msg),"ERROR: Rin parameter has wrong length %d\n",parmlen);
1066  if ((checkchangelog(log,lognamn))==0)
1067  n2log(log,msg);
1068  break;
1069  }
1070  x+=parmlen;
1071  #ifdef SETDBG
1072  puts("RINParameter");
1073  #endif
1074  check = MyOperation->setValue("RINParameter",(char*)&tmpcdrptr->RINParameter);
1075  if (check == -1)
1076  error_handler(MyTransaction->getNdbErrorString());
1077  break;
1078  case 253: /* OriginatingPointCode */
1079  switch (parmlen) {
1080  case 2:
1081  swab(buf+x,buf+x,2);
1082  memcpy(&tmpcdrptr->OriginatingPointCode,(buf+x),2);
1083  tmpcdrptr->USED_FIELDS |= B_OriginatingPointCode;
1084  break;
1085  case 3:
1086  swab(buf+x,buf+x,2);
1087  swab(buf+(x+1),buf+(x+1),2);
1088  swab(buf+x,buf+x,2);
1089  memcpy(&tmpcdrptr->OriginatingPointCode,(buf+x),3);
1090  tmpcdrptr->USED_FIELDS |= B_OriginatingPointCode;
1091  break;
1092  default :
1093  BaseString::snprintf(msg,sizeof(msg),"ERROR: OriginatingPointCode parameter has wrong length %d\n",parmlen);
1094  if ((checkchangelog(log,lognamn))==0)
1095  n2log(log,msg);
1096  break;
1097  }
1098  x+=parmlen;
1099  #ifdef SETDBG
1100  puts("OPC");
1101  #endif
1102  check = MyOperation->setValue("OPC",(char*)&tmpcdrptr->OriginatingPointCode);
1103  if (check == -1)
1104  error_handler(MyTransaction->getNdbErrorString());
1105  break;
1106  case 254: /* DestinationPointCode */
1107  switch (parmlen) {
1108  case 2:
1109  swab(buf+x,buf+x,2);
1110  memcpy(&tmpcdrptr->DestinationPointCode,(buf+x),2);
1111  /*
1112  tmpcdrptr->DestinationPointCode = buf[x] << 8;
1113  */
1114  tmpcdrptr->USED_FIELDS |= B_DestinationPointCode;
1115  break;
1116  case 3:
1117  swab(buf+x,buf+x,2);
1118  swab(buf+(x+1),buf+(x+1),2);
1119  swab(buf+x,buf+x,2);
1120  memcpy(&tmpcdrptr->DestinationPointCode,(buf+x),3);
1121  tmpcdrptr->USED_FIELDS |= B_DestinationPointCode;
1122  break;
1123  default :
1124  BaseString::snprintf(msg,sizeof(msg),"ERROR: DestinationPointCode parameter has wrong length %d\n",parmlen);
1125  if ((checkchangelog(log,lognamn))==0)
1126  n2log(log,msg);
1127  break;
1128  }
1129  x+=parmlen;
1130  #ifdef SETDBG
1131  puts("DPC");
1132  #endif
1133  check = MyOperation->setValue("DPC",(char*)&tmpcdrptr->DestinationPointCode);
1134  if (check == -1)
1135  error_handler(MyTransaction->getNdbErrorString());
1136  break;
1137  case 255: /* CircuitIdentificationCode */
1138  swab(buf+x,buf+x,2);
1139  memcpy(&tmpcdrptr->CircuitIdentificationCode,(buf+x),2);
1140  tmpcdrptr->USED_FIELDS |= B_CircuitIdentificationCode;
1141  x+=parmlen;
1142  #ifdef SETDBG
1143  puts("CIC");
1144  #endif
1145  check = MyOperation->setValue("CIC",(char*)&tmpcdrptr->CircuitIdentificationCode);
1146  if (check == -1)
1147  error_handler(MyTransaction->getNdbErrorString());
1148  break;
1149  default:
1150  printf("ERROR: Undefined parmtype %d , previous %d, length %d\n",parmtype,parmtype_prev,parmlen);
1151  BaseString::snprintf(msg,sizeof(msg),"ERROR: Undefined parmtype %d , previous %d, length %d\n",parmtype,parmtype_prev,parmlen);
1152  if ((checkchangelog(log,lognamn))==0)
1153  n2log(log,msg);
1154  if (parmlen == 0) {
1155  x++;
1156  }
1157  x+=parmlen;
1158  break;
1159  }
1160  parmtype_prev=parmtype;
1161  if ((cdrrestlen-x) == 1) {
1162  done=TRUE;
1163  }
1164  }
1165  time(&ourtime);
1166  if (ourtime != tmptime)
1167  {
1168  transfer = tmptransfer;
1169  tmptransfer = 0;
1170  if (++act_index == 30)
1171  {
1172  act_index = 0;
1173  printf("Transfer=%d\n",transfer);
1174  printf("Total operations=%d\n",reqcnt);
1175  printf("CAS1=%d\n",c1/30);
1176  printf("CAS2=%d\n",c2/30);
1177  printf("CAS3=%d\n",c3/30);
1178  c1=0;
1179  c2=0;
1180  c3=0;
1181  }
1182  tmptime = ourtime;
1183  }
1184  switch (cdrsubtype) {
1185  case 0:
1186  tmpcdrptr->ClientId = servernum;
1187  #ifdef SETDBG
1188  puts("ClientId");
1189  #endif
1190  check = MyOperation->setValue("ClientId",(char*)&tmpcdrptr->ClientId);
1191  if (check == -1)
1192  error_handler(MyTransaction->getNdbErrorString());
1193  tmpcdrptr->OurSTART_TIME = ourtime;
1194  #ifdef SETDBG
1195  puts("OurSTART_TIME");
1196  #endif
1197  check = MyOperation->setValue("OurSTART_TIME",(char*)&tmpcdrptr->OurSTART_TIME);
1198  if (check == -1)
1199  error_handler(MyTransaction->getNdbErrorString());
1200  tmpcdrptr->USED_FIELDS |= B_START_TIME;
1201  #ifdef SETDBG
1202  puts("USED_FIELDS");
1203  #endif
1204  check = MyOperation->setValue("USED_FIELDS",(char*)&tmpcdrptr->USED_FIELDS);
1205  if (check == -1)
1206  error_handler(MyTransaction->getNdbErrorString());
1207  break;
1208 
1209  case 1:
1210  tmpcdrptr->OurTimeForStartOfCharge = ourtime;
1211  #ifdef SETDBG
1212  puts("OurStartOfCharge");
1213  #endif
1214  check = MyOperation->setValue("OurStartOfCharge",(char*)&tmpcdrptr->OurTimeForStartOfCharge);
1215  if (check == -1)
1216  error_handler(MyTransaction->getNdbErrorString());
1217  tmpcdrptr->USED_FIELDS |= B_TimeForStartOfCharge;
1218  #ifdef SETDBG
1219  puts("USED_FIELDS");
1220  #endif
1221  check = MyOperation->setValue("USED_FIELDS",(char*)&tmpcdrptr->USED_FIELDS);
1222  if (check == -1)
1223  error_handler(MyTransaction->getNdbErrorString());
1224  break;
1225 
1226  case 2:
1227  tmpcdrptr->OurTimeForStopOfCharge = ourtime;
1228  #ifdef SETDBG
1229  puts("OurStopOfCharge");
1230  #endif
1231  check = MyOperation->setValue("OurStopOfCharge",(char*)&tmpcdrptr->OurTimeForStopOfCharge);
1232  if (check == -1)
1233  error_handler(MyTransaction->getNdbErrorString());
1234  tmpcdrptr->USED_FIELDS |= B_TimeForStopOfCharge;
1235  #ifdef SETDBG
1236  puts("USED_FIELDS");
1237  #endif
1238  check = MyOperation->setValue("USED_FIELDS",(char*)&tmpcdrptr->USED_FIELDS);
1239  if (check == -1)
1240  error_handler(MyTransaction->getNdbErrorString());
1241  break;
1242 
1243  case 3:
1244  tmpcdrptr->CallAttemptState = 4;
1245  break;
1246  default:
1247  snprintf(msg,sizeof(msg),"cdrtype %d unknown",cdrsubtype);
1248  if ((checkchangelog(log,lognamn))==0)
1249  n2log(log,msg);
1250  goto errout;
1251  break;
1252  }
1253  ops++;
1254  if (ops == ops_before_exe) {
1255  ops = 0;
1256  #ifdef SETDBG
1257  puts("Going to execute");
1258  #endif
1259  check = MyTransaction->execute(Commit, CommitAsMuchAsPossible);
1260  if ((check == -1) && (MyTransaction->getNdbError() != 0))
1261  error_handler(MyTransaction->getNdbErrorString());
1262  MyNdb.closeTransaction(MyTransaction);
1263  #ifdef SETDBG
1264  puts("Transaction closed");
1265  #endif
1266 
1267  #ifdef SETDBG
1268  puts("New transaction initiated");
1269  #endif
1270  }//if
1271  /* Increment the request count. */
1272  reqcnt++;
1273 
1274  /* Send a response back to the client. */
1275 
1276  /* if (send(s, buf, 10, 0) != 10) goto errout; */
1277  }
1278 
1279  /* The loop has terminated, because there are no */
1280  /* more requests to be serviced. As mentioned above, */
1281  /* this close will block until all of the sent replies */
1282  /* have been received by the remote host. The reason */
1283  /* for lingering on the close is so that the server will */
1284  /* have a better idea of when the remote has picked up */
1285  /* all of the data. This will allow the start and finish */
1286  /* times printed in the log file to reflect more accurately */
1287  /* the length of time this connection was */
1288  /* The port number must be converted first to host byte */
1289  /* order before printing. On most hosts, this is not */
1290  /* necessary, but the ntohs() call is included here so */
1291  /* that this program could easily be ported to a host */
1292  /* that does require it. */
1293 
1294  BaseString::snprintf(msg,sizeof(msg),"Completed %s port %u, %d requests",hostname,ntohs(peeraddr_in.sin_port), reqcnt);
1295  if ((checkchangelog(fi,temp))==0)
1296  c2log(fi,msg);
1297  error_from_client = 1;
1298  BaseString::snprintf(msg,sizeof(msg),"Communicate with threads");
1299  if ((checkchangelog(log,lognamn))==0)
1300  n2log(log,msg);
1301  BaseString::snprintf(msg,sizeof(msg),"Waiting for threads to return from work");
1302  if ((checkchangelog(log,lognamn))==0)
1303  n2log(log,msg);
1304  BaseString::snprintf(msg,sizeof(msg),"Closing down");
1305  if ((checkchangelog(log,lognamn))==0)
1306  n2log(log,msg);
1307  close(s);
1308  fclose(log);
1309  return EXIT_SUCCESS;
1310 
1311 errout:
1312  BaseString::snprintf(msg,sizeof(msg),"Connection with %s aborted on error\n", hostname);
1313  if ((checkchangelog(log,lognamn))==0)
1314  n2log(log,msg);
1315  if ((checkchangelog(fi,temp))==0)
1316  c2log(fi,msg);
1317  error_from_client = 1;
1318  BaseString::snprintf(msg,sizeof(msg),"Communicate with threads");
1319  if ((checkchangelog(log,lognamn))==0)
1320  n2log(log,msg);
1321  BaseString::snprintf(msg,sizeof(msg),"Waiting for threads to return from work");
1322  if ((checkchangelog(log,lognamn))==0)
1323  n2log(log,msg);
1324  BaseString::snprintf(msg,sizeof(msg),"Closing down");
1325  if ((checkchangelog(log,lognamn))==0)
1326  n2log(log,msg);
1327  close(s);
1328  fclose(log);
1329  return EXIT_FAILURE;
1330 }
1331 
1332 void
1333 create_table(Ndb* pMyNdb)
1334 {
1335 
1336  /****************************************************************
1337  * Create table and attributes.
1338  *
1339  * create table basictab1(
1340  * col1 int,
1341  * col2 int not null,
1342  * col3 int not null,
1343  * col4 int not null
1344  * )
1345  *
1346  ***************************************************************/
1347 
1348  int check;
1349  int i;
1350  NdbSchemaCon *MySchemaTransaction;
1351  NdbSchemaOp *MySchemaOp;
1352  int tAttributeSize;
1353 
1354  tAttributeSize = 1;
1355 
1356  cout << "Creating " << tableName << "..." << endl;
1357 
1358  MySchemaTransaction = pMyNdb->startSchemaTransaction();
1359  if( MySchemaTransaction == NULL )
1360  error_handler(MySchemaTransaction->getNdbErrorString());
1361 
1362  MySchemaOp = MySchemaTransaction->getNdbSchemaOp();
1363  if( MySchemaOp == NULL )
1364  error_handler(MySchemaTransaction->getNdbErrorString());
1365 
1366  // Createtable
1367  check = MySchemaOp->createTable( tableName,
1368  8, // Table Size
1369  TupleKey, // Key Type
1370  40 // Nr of Pages
1371  );
1372  if( check == -1 )
1373  error_handler(MySchemaTransaction->getNdbErrorString());
1374 
1375  // CallIdentificationNumber Create first column, primary key
1376  check = MySchemaOp->createAttribute( "CIN",
1377  TupleKey,
1378  32,
1379  tAttributeSize,
1380  UnSigned, MMBased,
1381  NotNullAttribute
1382  );
1383  if( check == -1 )
1384  error_handler(MySchemaTransaction->getNdbErrorString());
1385 
1386 
1387  // USED_FIELDS Create attributes
1388  check = MySchemaOp->createAttribute( "USED_FIELDS", NoKey, 32,
1389  tAttributeSize, UnSigned, MMBased,
1390  NullAttribute );
1391  if( check == -1 )
1392  error_handler(MySchemaTransaction->getNdbErrorString());
1393 
1394  // ClientId Create attributes
1395  check = MySchemaOp->createAttribute( "ClientId", NoKey, 32,
1396  tAttributeSize, UnSigned, MMBased,
1397  NullAttribute );
1398  if( check == -1 )
1399  error_handler(MySchemaTransaction->getNdbErrorString());
1400 
1401  // START_TIME Create attributes
1402  check = MySchemaOp->createAttribute( "START_TIME", NoKey, 32,
1403  tAttributeSize, UnSigned, MMBased,
1404  NullAttribute );
1405  if( check == -1 )
1406  error_handler(MySchemaTransaction->getNdbErrorString());
1407 
1408  // OurSTART_TIME Create attributes
1409  check = MySchemaOp->createAttribute( "OurSTART_TIME", NoKey, 32,
1410  tAttributeSize, UnSigned, MMBased,
1411  NullAttribute );
1412  if( check == -1 )
1413  error_handler(MySchemaTransaction->getNdbErrorString());
1414 
1415  // TimeForStartOfCharge Create attributes
1416  check = MySchemaOp->createAttribute( "StartOfCharge", NoKey, 32,
1417  tAttributeSize, UnSigned, MMBased,
1418  NullAttribute );
1419  if( check == -1 )
1420  error_handler(MySchemaTransaction->getNdbErrorString());
1421 
1422  // TimeForStopOfCharge Create attributes
1423  check = MySchemaOp->createAttribute( "StopOfCharge", NoKey, 32,
1424  tAttributeSize, UnSigned, MMBased,
1425  NullAttribute );
1426  if( check == -1 )
1427  error_handler(MySchemaTransaction->getNdbErrorString());
1428 
1429  // OurTimeForStartOfCharge Create attributes
1430  check = MySchemaOp->createAttribute( "OurStartOfCharge", NoKey, 32,
1431  tAttributeSize, UnSigned, MMBased,
1432  NullAttribute );
1433  if( check == -1 )
1434  error_handler(MySchemaTransaction->getNdbErrorString());
1435 
1436  // OurTimeForStopOfCharge Create attributes
1437  check = MySchemaOp->createAttribute( "OurStopOfCharge", NoKey, 32,
1438  tAttributeSize, UnSigned, MMBased,
1439  NullAttribute );
1440  if( check == -1 )
1441  error_handler(MySchemaTransaction->getNdbErrorString());
1442 
1443  // DestinationPointCode Create attributes
1444  check = MySchemaOp->createAttribute( "DPC", NoKey, 16,
1445  tAttributeSize, UnSigned, MMBased,
1446  NullAttribute );
1447  if( check == -1 )
1448  error_handler(MySchemaTransaction->getNdbErrorString());
1449 
1450  // OriginatingPointCode Create attributes
1451  check = MySchemaOp->createAttribute( "OPC", NoKey, 16,
1452  tAttributeSize, UnSigned, MMBased,
1453  NullAttribute );
1454  if( check == -1 )
1455  error_handler(MySchemaTransaction->getNdbErrorString());
1456 
1457  // CircuitIdentificationCode Create attributes
1458  check = MySchemaOp->createAttribute( "CIC", NoKey, 16,
1459  tAttributeSize, UnSigned, MMBased,
1460  NullAttribute );
1461  if( check == -1 )
1462  error_handler(MySchemaTransaction->getNdbErrorString());
1463 
1464  // ReroutingIndicator Create attributes
1465  check = MySchemaOp->createAttribute( "RI", NoKey, 16,
1466  tAttributeSize, UnSigned, MMBased,
1467  NullAttribute );
1468  if( check == -1 )
1469  error_handler(MySchemaTransaction->getNdbErrorString());
1470 
1471  // RINParameter Create attributes
1472  check = MySchemaOp->createAttribute( "RINParameter", NoKey, 16,
1473  tAttributeSize, UnSigned, MMBased,
1474  NullAttribute );
1475  if( check == -1 )
1476  error_handler(MySchemaTransaction->getNdbErrorString());
1477 
1478  // NetworkIndicator Create attributes
1479  check = MySchemaOp->createAttribute( "NIndicator", NoKey, 8,
1480  tAttributeSize, Signed, MMBased,
1481  NullAttribute );
1482  if( check == -1 )
1483  error_handler(MySchemaTransaction->getNdbErrorString());
1484 
1485  // CallAttemptState Create attributes
1486  check = MySchemaOp->createAttribute( "CAS", NoKey, 8,
1487  tAttributeSize, Signed, MMBased,
1488  NullAttribute );
1489  if( check == -1 )
1490  error_handler(MySchemaTransaction->getNdbErrorString());
1491 
1492  // ACategory Create attributes
1493  check = MySchemaOp->createAttribute( "ACategory", NoKey, 8,
1494  tAttributeSize, Signed, MMBased,
1495  NullAttribute );
1496  if( check == -1 )
1497  error_handler(MySchemaTransaction->getNdbErrorString());
1498 
1499  // EndOfSelectionInformation Create attributes
1500  check = MySchemaOp->createAttribute( "EndOfSelInf", NoKey, 8,
1501  tAttributeSize, Signed, MMBased,
1502  NullAttribute );
1503  if( check == -1 )
1504  error_handler(MySchemaTransaction->getNdbErrorString());
1505 
1506  // UserToUserInformation Create attributes
1507  check = MySchemaOp->createAttribute( "UserToUserInf", NoKey, 8,
1508  tAttributeSize, Signed, MMBased,
1509  NullAttribute );
1510  if( check == -1 )
1511  error_handler(MySchemaTransaction->getNdbErrorString());
1512 
1513  // UserToUserIndicator Create attributes
1514  check = MySchemaOp->createAttribute( "UserToUserInd", NoKey, 8,
1515  tAttributeSize, Signed, MMBased,
1516  NullAttribute );
1517  if( check == -1 )
1518  error_handler(MySchemaTransaction->getNdbErrorString());
1519 
1520  // CauseCode Create attributes
1521  check = MySchemaOp->createAttribute( "CauseCode", NoKey, 8,
1522  tAttributeSize, Signed, MMBased,
1523  NullAttribute );
1524  if( check == -1 )
1525  error_handler(MySchemaTransaction->getNdbErrorString());
1526 
1527  // ASubscriberNumber attributes
1528  check = MySchemaOp->createAttribute( "ANumber", NoKey, 8,
1529  ASubscriberNumber_SIZE, Signed, MMBased,
1530  NullAttribute );
1531  if( check == -1 )
1532  error_handler(MySchemaTransaction->getNdbErrorString());
1533 
1534  // ASubscriberNumberLenght attributes
1535  check = MySchemaOp->createAttribute( "ANumberLength", NoKey, 8,
1536  tAttributeSize, Signed, MMBased,
1537  NullAttribute );
1538  if( check == -1 )
1539  error_handler(MySchemaTransaction->getNdbErrorString());
1540 
1541  // TonASubscriberNumber attributes
1542  check = MySchemaOp->createAttribute( "TonANumber", NoKey, 8,
1543  tAttributeSize, Signed, MMBased,
1544  NullAttribute );
1545  if( check == -1 )
1546  error_handler(MySchemaTransaction->getNdbErrorString());
1547 
1548  // BSubscriberNumber attributes
1549  check = MySchemaOp->createAttribute( "BNumber", NoKey, 8,
1550  BSubscriberNumber_SIZE, Signed, MMBased,
1551  NullAttribute );
1552  if( check == -1 )
1553  error_handler(MySchemaTransaction->getNdbErrorString());
1554 
1555  // BSubscriberNumberLength attributes
1556  check = MySchemaOp->createAttribute( "BNumberLength", NoKey, 8,
1557  tAttributeSize, Signed, MMBased,
1558  NullAttribute );
1559  if( check == -1 )
1560  error_handler(MySchemaTransaction->getNdbErrorString());
1561 
1562  // TonBSubscriberNumber attributes
1563  check = MySchemaOp->createAttribute( "TonBNumber", NoKey, 8,
1564  tAttributeSize, Signed, MMBased,
1565  NullAttribute );
1566  if( check == -1 )
1567  error_handler(MySchemaTransaction->getNdbErrorString());
1568 
1569  // RedirectingNumber attributes
1570  check = MySchemaOp->createAttribute( "RNumber", NoKey, 8,
1571  ASubscriberNumber_SIZE, Signed, MMBased,
1572  NullAttribute );
1573  if( check == -1 )
1574  error_handler(MySchemaTransaction->getNdbErrorString());
1575 
1576  // TonRedirectingNumber attributes
1577  check = MySchemaOp->createAttribute( "TonRNumber", NoKey, 8,
1578  tAttributeSize, Signed, MMBased,
1579  NullAttribute );
1580  if( check == -1 )
1581  error_handler(MySchemaTransaction->getNdbErrorString());
1582 
1583  // OriginalCalledNumber attributes
1584  check = MySchemaOp->createAttribute( "ONumber", NoKey, 8,
1585  ASubscriberNumber_SIZE, Signed, MMBased,
1586  NullAttribute );
1587  if( check == -1 )
1588  error_handler(MySchemaTransaction->getNdbErrorString());
1589 
1590  // TonOriginalCalledNumber attributes
1591  check = MySchemaOp->createAttribute( "TonONumber", NoKey, 8,
1592  tAttributeSize, Signed, MMBased,
1593  NullAttribute );
1594  if( check == -1 )
1595  error_handler(MySchemaTransaction->getNdbErrorString());
1596 
1597  // LocationCode attributes
1598  check = MySchemaOp->createAttribute( "LocationCode", NoKey, 8,
1599  ASubscriberNumber_SIZE, Signed, MMBased,
1600  NullAttribute );
1601  if( check == -1 )
1602  error_handler(MySchemaTransaction->getNdbErrorString());
1603 
1604  // TonLocationCode attributes
1605  check = MySchemaOp->createAttribute( "TonLocationCode", NoKey, 8,
1606  tAttributeSize, Signed, MMBased,
1607  NullAttribute );
1608  if( check == -1 )
1609  error_handler(MySchemaTransaction->getNdbErrorString());
1610 
1611  if( MySchemaTransaction->execute() == -1 ) {
1612  cout << tableName << " already exist" << endl;
1613  cout << "Message: " << MySchemaTransaction->getNdbErrorString() << endl;
1614  }
1615  else
1616  {
1617  cout << tableName << " created" << endl;
1618  }
1619  pMyNdb->closeSchemaTransaction(MySchemaTransaction);
1620 
1621  return;
1622 }
1623 
1624 void
1625 error_handler(const char* errorText)
1626 {
1627  // Test failed
1628  cout << endl << "ErrorMessage: " << errorText << endl;
1629  bTestPassed = -1;
1630 }