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

UDPAPIClientTest.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 "UDPAPIClientTest.h"
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <arpa/inet.h>
00030 
00031 
00032 char sendBuff[] = "I'm Alive.";
00033 
00034 FILE *fp;
00035 
00036 void
00037 UDPClientTestInit()
00038 {
00039   TSCont cont;
00040   unsigned long ip;
00041   TSMutex readMutexp;
00042 
00043   ip = inet_addr("209.131.48.79");
00044   readMutexp = TSMutexCreate();
00045   cont = TSContCreate(&UDPClient_handle_callbacks, readMutexp);
00046   fp = fopen("UDPAPI.dbg", "a+");
00047   fprintf(fp, "UDPClient Init called\n");
00048   fclose(fp);
00049   INKUDPBind(cont, ip, 9999);
00050 
00051 }
00052 
00053 int
00054 UDPClient_handle_callbacks(TSCont cont, TSEvent event, void *e)
00055 {
00056   INKUDPacketQueue packetQueue;
00057   INKUDPPacket packet;
00058   TSIOBufferBlock recvBuffBlock;
00059   unsigned int destIp = inet_addr("209.131.48.79");
00060   int destPort = 1813;
00061   INKUDPConn UDPConn;
00062   TSIOBufferReader reader;
00063   TSIOBuffer iobuffer;
00064   const char *buf;
00065   int avail, total_len = 0;
00066   char recvBuff[1024];
00067   fp = fopen("UDPAPI.dbg", "a+");
00068 
00069   switch (event) {
00070 
00071   case TS_NET_EVENT_DATAGRAM_OPEN:
00072     UDPConn = (INKUDPConn) e;
00073     INKUDPRecvFrom(cont, UDPConn);
00074     INKUDPSendTo(cont, UDPConn, destIp, destPort, sendBuff, strlen(sendBuff));
00075     fprintf(fp, "sent %s\n.", (const char *) sendBuff);
00076 
00077     break;
00078 
00079   case TS_NET_EVENT_DATAGRAM_READ_READY:
00080     fprintf(fp, "read ready called\n.");
00081     packetQueue = (INKUDPacketQueue) e;
00082 
00083     while ((packet = INKUDPPacketGet(packetQueue)) != NULL) {
00084       recvBuffBlock = INKUDPPacketBufferBlockGet(packet);
00085 
00086       iobuffer = TSIOBufferCreate();
00087       reader = TSIOBufferReaderAlloc(iobuffer);
00088       TSIOBufferAppend(iobuffer, recvBuffBlock);
00089       buf = TSIOBufferBlockReadStart(recvBuffBlock, reader, &avail);
00090 
00091       if (avail > 0) {
00092 
00093         for (int i = 0; i < avail; i++)
00094           fprintf(fp, "%c", *(buf + i));
00095 
00096 
00097         memcpy((char *) &recvBuff + total_len, buf, avail);
00098         TSIOBufferReaderConsume(reader, avail);
00099         total_len += avail;
00100       }
00101 
00102       /* INKqa10255: we'd free the memory  - jinsheng */
00103       INKUDPPacketDestroy(packet);
00104       TSIOBufferReaderFree(reader);
00105       TSIOBufferDestroy(iobuffer);
00106     }
00107 
00108     break;
00109 
00110   case TS_NET_EVENT_DATAGRAM_WRITE_COMPLETE:
00111     break;
00112 
00113   }
00114   fclose(fp);
00115   return TS_EVENT_CONTINUE;
00116 }

Generated by  doxygen 1.7.1