DCS_TEAM / GPRS-server

Fork of GPRSInterface by -deleted-

Socket/Endpoint.cpp

Committer:
lawliet
Date:
2014-02-25
Revision:
0:8ccbd963e74d

File content as of revision 0:8ccbd963e74d:

/*
  Endpoint.cpp
  2014 Copyright (c) Seeed Technology Inc.  All right reserved.

  Author:lawliet zou(lawliet.zou@gmail.com)
  2014-2-24

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include "Socket/Socket.h"
#include "Socket/Endpoint.h"

Endpoint::Endpoint()
{
    reset_address();
}
Endpoint::~Endpoint() {}

void Endpoint::reset_address(void)
{
    _ipAddress[0] = '\0';
    _port = 0;
}

int Endpoint::set_address(const char* host, const int port)
{
    //Resolve DNS address or populate hard-coded IP address
    GPRS* gprs = GPRS::getInstance();
    if (gprs == NULL) {
        return -1;
    }
    uint32_t addr;
    if (!gprs->gethostbyname(host, &addr)) {
        return -1;
    }
    snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
    _port = port;
    return 0;
}

char* Endpoint::get_address()
{
    return _ipAddress;
}

int Endpoint::get_port()
{
    return _port;
}