udp_client.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Yaskawa America, Inc.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *       * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *       * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *       * Neither the name of the Yaskawa America, Inc., nor the names
00016  *       of its contributors may be used to endorse or promote products derived
00017  *       from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 #ifdef ROS
00032 #include "simple_message/socket/udp_client.h"
00033 #include "simple_message/log_wrapper.h"
00034 #endif
00035 
00036 #ifdef MOTOPLUS
00037 #include "udp_client.h"
00038 #include "log_wrapper.h"
00039 #endif
00040 
00041 
00042 using namespace industrial::byte_array;
00043 using namespace industrial::shared_types;
00044 
00045 namespace industrial
00046 {
00047 namespace udp_client
00048 {
00049 
00050 UdpClient::UdpClient()
00051 {
00052 }
00053 
00054 UdpClient::~UdpClient()
00055 {
00056 }
00057 
00058 bool UdpClient::init(char *buff, int port_num)
00059 {
00060 
00061   int rc;
00062   bool rtn;
00063 
00064   /* Create a socket using:
00065    * AF_INET - IPv4 internet protocol
00066    * SOCK_DGRAM - UDP type
00067    * protocol (0) - System chooses
00068    */
00069   rc = SOCKET(AF_INET, SOCK_DGRAM, 0);
00070   if (this->SOCKET_FAIL != rc)
00071   {
00072     this->setSockHandle(rc);
00073 
00074     // Initialize address data structure
00075     memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00076     this->sockaddr_.sin_family = AF_INET;
00077     this->sockaddr_.sin_addr.s_addr = INET_ADDR(buff);
00078     this->sockaddr_.sin_port = HTONS(port_num);
00079 
00080     rtn = true;
00081 
00082   }
00083   else
00084   {
00085     LOG_ERROR("Failed to create socket, rc: %d", rc);
00086     rtn = false;
00087   }
00088   return rtn;
00089 }
00090 
00091 
00092 bool UdpClient::makeConnect()
00093 {
00094   ByteArray send;
00095   char sendHS = this->CONNECT_HANDSHAKE;
00096   char recvHS = 0;
00097   bool rtn = false;
00098   const int timeout = 1000;  // Time (ms) between handshake sends
00099   int bytesRcvd = 0;
00100   
00101   if (!this->isConnected())
00102   {
00103     this->setConnected(false);
00104     send.load((void*)&sendHS, sizeof(sendHS));
00105   
00106     do
00107     {
00108       ByteArray recv;
00109       recvHS = 0;
00110       LOG_DEBUG("UDP client sending handshake");
00111       this->rawSendBytes(send.getRawDataPtr(), send.getBufferSize());
00112       if (this->isReadyReceive(timeout))
00113       {
00114         bytesRcvd = this->rawReceiveBytes(this->buffer_, 0);
00115         LOG_DEBUG("UDP client received possible handshake");    
00116         recv.init(&this->buffer_[0], bytesRcvd);
00117         recv.unload((void*)&recvHS, sizeof(recvHS));
00118       }
00119     }
00120     while(recvHS != sendHS);
00121     LOG_INFO("UDP client connected");
00122     rtn = true;
00123     this->setConnected(true);
00124     
00125   }
00126   else
00127   {
00128     rtn = true;
00129     LOG_WARN("Tried to connect when socket already in connected state");
00130   }
00131 
00132   return rtn;
00133 }
00134 } //udp_client
00135 } //industrial
00136 


simple_message
Author(s): Shaun Edwards
autogenerated on Mon Oct 6 2014 00:54:18