Final Code 12/19/2015

Dependents:   DCS_FINAL_CODE

Files at this revision

API Documentation at this revision

Comitter:
DeWayneDennis
Date:
Sat Dec 19 21:57:34 2015 +0000
Commit message:
Final Code 12/19/2015

Changed in this revision

Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket.h Show annotated file Show diff for this revision Revisions of this file
TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b49935f48132 Socket.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket.cpp	Sat Dec 19 21:57:34 2015 +0000
@@ -0,0 +1,42 @@
+/*
+  Socket.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.h"
+
+Socket::Socket() : _sock_fd(-1) {
+    gprs = GPRS::getInstance();
+    if (gprs == NULL) {
+        error("Socket constructor error: no GPRS instance available!\r\n");
+    }       
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout)
+{
+    //nothing to do for GPRS   
+}
+int Socket::close() {
+    return (gprs->close(_sock_fd)) ? 0 : -1;
+}
+
+Socket::~Socket() {
+    close();
+}
diff -r 000000000000 -r b49935f48132 Socket.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket.h	Sat Dec 19 21:57:34 2015 +0000
@@ -0,0 +1,49 @@
+/*
+  Socket.h
+  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
+*/
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+#include "GPRS.h"
+
+/** Socket file descriptor and select wrapper
+  */
+class Socket {
+public:
+    /** Socket
+     */
+    Socket();
+    
+    void set_blocking(bool blocking, unsigned int timeout=1);
+      
+    /** Close the socket file descriptor
+     */
+    int close();
+    
+    ~Socket();
+    
+protected:
+    int _sock_fd;
+    GPRS* gprs;
+};
+
+
+#endif /* SOCKET_H_ */
diff -r 000000000000 -r b49935f48132 TCPSocketConnection.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCPSocketConnection.cpp	Sat Dec 19 21:57:34 2015 +0000
@@ -0,0 +1,95 @@
+/*
+  TCPSocketConnection.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 "TCPSocketConnection.h"
+
+TCPSocketConnection::TCPSocketConnection()
+{
+}
+
+int TCPSocketConnection::connect(const char* host, const int port)
+{
+    printf(">%s<",host);
+    if (_sock_fd < 0) {
+        _sock_fd = gprs->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
+
+    if (!gprs->connect(_sock_fd, TCP, host, port)) {
+        return false;
+    }
+    return true;
+}
+
+bool TCPSocketConnection::is_connected(void)
+{
+    return gprs->is_connected(_sock_fd);
+}
+
+int TCPSocketConnection::send(char* data, int length)
+{   
+    printf("Current Socket is %d \r\n", _sock_fd);
+    int size = gprs->wait_writeable(_sock_fd, length);
+    if (size < 0) {
+        return -1;
+    }
+    if (size > length) {
+        size = length;
+    }
+    return gprs->send(_sock_fd, data, size);
+}
+
+int TCPSocketConnection::send_all(char* data, int length)
+{
+    return send(data,length);
+}
+void TCPSocketConnection::setSocket(int currentSocket)
+{
+    _sock_fd = currentSocket;
+}
+int TCPSocketConnection::receive(char* data, int length)
+{
+#if 0
+    if (size < 0) {
+        return -1;
+    }
+    if(size == 0) {
+        size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
+    }
+
+    if(size > length) {
+        size = size - length;
+    } else {
+        length = size;
+        size = -1;
+    }
+#endif
+    int size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
+    return gprs->recv(_sock_fd, data, size>length?length:size);
+}
+
+int TCPSocketConnection::receive_all(char* data, int length)
+{
+    return receive(data,length);
+}
diff -r 000000000000 -r b49935f48132 TCPSocketConnection.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCPSocketConnection.h	Sat Dec 19 21:57:34 2015 +0000
@@ -0,0 +1,83 @@
+/*
+  TCPSocketConnection.h
+  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
+*/
+
+#ifndef TCPSOCKET_H
+#define TCPSOCKET_H
+
+#include "Socket.h"
+
+/** TCP socket connection
+ */
+class TCPSocketConnection: public Socket
+{
+    friend class TCPSocketServer;
+
+public:
+    /** TCP socket connection
+    */
+    TCPSocketConnection();
+
+    /** Connects this TCP socket to the server
+    \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
+    \param port The host's port to connect to.
+    \return 0 on success, -1 on failure.
+    */
+    int connect(const char* host, const int port);
+
+    /** Check if the socket is connected
+    \return true if connected, false otherwise.
+    */
+    bool is_connected(void);
+
+    /** Send data to the remote host.
+    \param data The buffer to send to the host.
+    \param length The length of the buffer to send.
+    \return the number of written bytes on success (>=0) or -1 on failure
+     */
+    int send(char* data, int length);
+
+    /** Send all the data to the remote host.
+    \param data The buffer to send to the host.
+    \param length The length of the buffer to send.
+    \return the number of written bytes on success (>=0) or -1 on failure
+    */
+    int send_all(char* data, int length);
+
+    /** Receive data from the remote host.
+    \param data The buffer in which to store the data received from the host.
+    \param length The maximum length of the buffer.
+    \return the number of received bytes on success (>=0) or -1 on failure
+     */
+    int receive(char* data, int length);
+
+    /** Receive all the data from the remote host.
+    \param data The buffer in which to store the data received from the host.
+    \param length The maximum length of the buffer.
+    \return the number of received bytes on success (>=0) or -1 on failure
+    */
+    int receive_all(char* data, int length);
+    
+    /* set the current socket */
+    void setSocket(int currentSocket);
+};
+
+#endif
diff -r 000000000000 -r b49935f48132 TCPSocketServer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCPSocketServer.h	Sat Dec 19 21:57:34 2015 +0000
@@ -0,0 +1,57 @@
+/*
+  TCPSocketServer.h
+  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
+*/
+#ifndef TCPSOCKETSERVER_H
+#define TCPSOCKETSERVER_H
+
+#include "Socket/Socket.h"
+#include "TCPSocketConnection.h"
+
+/** TCP Server.
+  */
+class TCPSocketServer : public Socket
+{
+public:
+    /** Instantiate a TCP Server.
+    */
+    TCPSocketServer();
+
+    /** Bind a socket to a specific port.
+    \param port The port to listen for incoming connections on.
+    \return 0 on success, -1 on failure.
+    */
+    int bind(int port);
+
+    /** Start listening for incoming connections.
+    \param backlog number of pending connections that can be queued up at any
+                   one time [Default: 1].
+    \return 0 on success, -1 on failure.
+    */
+    int listen(int backlog=1);
+
+    /** Accept a new connection.
+    \param connection A TCPSocketConnection instance that will handle the incoming connection.
+    \return 0 on success, -1 on failure.
+    */
+    int accept(TCPSocketConnection& connection);
+};
+
+#endif