• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

UDPAPITest.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include "UDPAPITest.h"
00025 #include <arpa/inet.h>
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 
00030 
00031 char ACK[] = "I got it.";
00032 
00033 FILE *fp;
00034 
00035 void
00036 UDPTestInit()
00037 {
00038   TSCont cont;
00039   TSMutex readMutexp;
00040   unsigned long ip;
00041 
00042   ip = inet_addr("209.131.48.79");
00043   readMutexp = TSMutexCreate();
00044   cont = TSContCreate(&handle_callbacks, readMutexp);
00045 //      INKUDPBind(cont, INADDR_ANY,1813);
00046   INKUDPBind(cont, ip, 1813);
00047 }
00048 
00049 
00050 void
00051 printN(const char *start, int length)
00052 {
00053   int i;
00054   for (i = 0; i < length; i++)
00055     fprintf(fp, "%c", *(start + i));
00056   fprintf(fp, "\n");
00057 }
00058 
00059 int
00060 handle_callbacks(TSCont cont, TSEvent event, void *e)
00061 {
00062   INKUDPacketQueue packetQueue;
00063   INKUDPPacket packet;
00064   TSIOBufferBlock recvBuffBlock;
00065   TSIOBufferReader reader;
00066   TSIOBuffer iobuffer;
00067   INKUDPConn UDPConn;
00068   unsigned int ip;
00069   int port;
00070   int *sizep;
00071   int size;
00072   char sendBuff[32];
00073   const char *buf;
00074   int avail, total_len;
00075   char recv_buffer[4096];
00076 
00077 
00078   fp = fopen("UDPServer.log", "a+");
00079 
00080   switch (event) {
00081 
00082   case TS_NET_EVENT_DATAGRAM_OPEN:
00083     fprintf(fp, "open event called\n");
00084     UDPConn = (INKUDPConn) e;
00085     INKUDPRecvFrom(cont, UDPConn);
00086     break;
00087 
00088 
00089   case TS_NET_EVENT_DATAGRAM_READ_READY:
00090     fprintf(fp, "read ready event called\n");
00091     packetQueue = (INKUDPacketQueue) e;
00092     total_len = 0;
00093     while ((packet = INKUDPPacketGet(packetQueue)) != NULL) {
00094       recvBuffBlock = INKUDPPacketBufferBlockGet(packet);
00095       iobuffer = TSIOBufferCreate();
00096       reader = TSIOBufferReaderAlloc(iobuffer);
00097       TSIOBufferAppend(iobuffer, recvBuffBlock);
00098       buf = TSIOBufferBlockReadStart(recvBuffBlock, reader, &avail);
00099 
00100       if (avail > 0) {
00101         fprintf(fp, "Received message is\n");
00102         printN(buf, avail);
00103         fprintf(fp, "message length = %i\n", avail);
00104         memcpy((char *) &recv_buffer + total_len, buf, avail);
00105         TSIOBufferReaderConsume(reader, avail);
00106         total_len += avail;
00107       }
00108 
00109 
00110       ip = INKUDPPacketFromAddressGet(packet);
00111       port = INKUDPPacketFromPortGet(packet);
00112       fprintf(fp, "port = %d\n", port);
00113 
00114       UDPConn = (INKUDPConn) INKUDPPacketConnGet(packet);
00115       INKUDPSendTo(cont, UDPConn, ip, port, ACK, strlen(ACK));
00116 
00117       /* INKqa10255: we'd free the memory  - jinsheng */
00118       INKUDPPacketDestroy(packet);
00119       TSIOBufferReaderFree(reader);
00120       TSIOBufferDestroy(iobuffer);
00121     }
00122 
00123     break;
00124 
00125   case TS_NET_EVENT_DATAGRAM_WRITE_COMPLETE:
00126     break;
00127 
00128   }
00129   fclose(fp);
00130   return TS_EVENT_CONTINUE;
00131 }

Generated by  doxygen 1.7.1