Changed names of the Ethernet classes: Client -> EthernetClient, NetClient -> Client, and basic testing performed

This commit is contained in:
amcewen 2011-08-29 22:36:28 +01:00
parent b7533c1839
commit ad5dead85a
20 changed files with 89 additions and 89 deletions

View File

@ -1,10 +1,10 @@
#ifndef netclient_h
#define netclient_h
#ifndef client_h
#define client_h
#include "Print.h"
#include "NetClient.h"
#include "Stream.h"
#include "IPAddress.h"
class NetClient : public Stream {
class Client : public Stream {
public:
virtual int connect(IPAddress ip, uint16_t port) =0;
@ -20,6 +20,8 @@ public:
virtual void stop() = 0;
virtual uint8_t connected() = 0;
virtual operator bool() = 0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
};
#endif

View File

@ -1,11 +0,0 @@
#ifndef netserver_h
#define netserver_h
class NetClient;
class NetServer {
public:
virtual void begin() =0;
};
#endif

View File

@ -0,0 +1,9 @@
#ifndef server_h
#define server_h
class Server {
public:
virtual void begin() =0;
};
#endif

View File

@ -4,8 +4,8 @@
#include <inttypes.h>
//#include "w5100.h"
#include "IPAddress.h"
#include "Client.h"
#include "Server.h"
#include "EthernetClient.h"
#include "EthernetServer.h"
#define MAX_SOCK_NUM 4
@ -29,8 +29,8 @@ public:
IPAddress gatewayIP();
IPAddress dnsServerIP();
friend class Client;
friend class Server;
friend class EthernetClient;
friend class EthernetServer;
};
extern EthernetClass Ethernet;

View File

@ -8,19 +8,19 @@ extern "C" {
#include "Arduino.h"
#include "Ethernet.h"
#include "Client.h"
#include "Server.h"
#include "EthernetClient.h"
#include "EthernetServer.h"
#include "Dns.h"
uint16_t Client::_srcport = 1024;
uint16_t EthernetClient::_srcport = 1024;
Client::Client() : _sock(MAX_SOCK_NUM) {
EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) {
}
Client::Client(uint8_t sock) : _sock(sock) {
EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) {
}
int Client::connect(const char* host, uint16_t port) {
int EthernetClient::connect(const char* host, uint16_t port) {
// Look up the host first
int ret = 0;
DNSClient dns;
@ -35,7 +35,7 @@ int Client::connect(const char* host, uint16_t port) {
}
}
int Client::connect(IPAddress ip, uint16_t port) {
int EthernetClient::connect(IPAddress ip, uint16_t port) {
if (_sock != MAX_SOCK_NUM)
return 0;
@ -54,7 +54,7 @@ int Client::connect(IPAddress ip, uint16_t port) {
if (_srcport == 0) _srcport = 1024;
socket(_sock, SnMR::TCP, _srcport, 0);
if (!::connect(_sock, ip.raw_address(), port)) {
if (!::connect(_sock, rawIPAddress(ip), port)) {
_sock = MAX_SOCK_NUM;
return 0;
}
@ -70,15 +70,15 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1;
}
size_t Client::write(uint8_t b) {
size_t EthernetClient::write(uint8_t b) {
return write(&b, 1);
}
size_t Client::write(const char *str) {
size_t EthernetClient::write(const char *str) {
return write((const uint8_t *) str, strlen(str));
}
size_t Client::write(const uint8_t *buf, size_t size) {
size_t EthernetClient::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) {
setWriteError();
return 0;
@ -90,13 +90,13 @@ size_t Client::write(const uint8_t *buf, size_t size) {
return size;
}
int Client::available() {
int EthernetClient::available() {
if (_sock != MAX_SOCK_NUM)
return W5100.getRXReceivedSize(_sock);
return 0;
}
int Client::read() {
int EthernetClient::read() {
uint8_t b;
if ( recv(_sock, &b, 1) > 0 )
{
@ -110,11 +110,11 @@ int Client::read() {
}
}
int Client::read(uint8_t *buf, size_t size) {
int EthernetClient::read(uint8_t *buf, size_t size) {
return recv(_sock, buf, size);
}
int Client::peek() {
int EthernetClient::peek() {
uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must
if (!available())
@ -123,12 +123,12 @@ int Client::peek() {
return b;
}
void Client::flush() {
void EthernetClient::flush() {
while (available())
read();
}
void Client::stop() {
void EthernetClient::stop() {
if (_sock == MAX_SOCK_NUM)
return;
@ -148,7 +148,7 @@ void Client::stop() {
_sock = MAX_SOCK_NUM;
}
uint8_t Client::connected() {
uint8_t EthernetClient::connected() {
if (_sock == MAX_SOCK_NUM) return 0;
uint8_t s = status();
@ -156,14 +156,14 @@ uint8_t Client::connected() {
(s == SnSR::CLOSE_WAIT && !available()));
}
uint8_t Client::status() {
uint8_t EthernetClient::status() {
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
return W5100.readSnSR(_sock);
}
// the next function allows us to use the client returned by
// Server::available() as the condition in an if-statement.
// EthernetServer::available() as the condition in an if-statement.
Client::operator bool() {
EthernetClient::operator bool() {
return _sock != MAX_SOCK_NUM;
}

View File

@ -1,15 +1,15 @@
#ifndef client_h
#define client_h
#ifndef ethernetclient_h
#define ethernetclient_h
#include "Arduino.h"
#include "Print.h"
#include "NetClient.h"
#include "Client.h"
#include "IPAddress.h"
class Client : public NetClient {
class EthernetClient : public Client {
public:
Client();
Client(uint8_t sock);
EthernetClient();
EthernetClient(uint8_t sock);
uint8_t status();
virtual int connect(IPAddress ip, uint16_t port);
@ -26,7 +26,7 @@ public:
virtual uint8_t connected();
virtual operator bool();
friend class Server;
friend class EthernetServer;
private:
static uint16_t _srcport;

View File

@ -5,18 +5,18 @@ extern "C" {
}
#include "Ethernet.h"
#include "Client.h"
#include "Server.h"
#include "EthernetClient.h"
#include "EthernetServer.h"
Server::Server(uint16_t port)
EthernetServer::EthernetServer(uint16_t port)
{
_port = port;
}
void Server::begin()
void EthernetServer::begin()
{
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock);
EthernetClient client(sock);
if (client.status() == SnSR::CLOSED) {
socket(sock, SnMR::TCP, _port, 0);
listen(sock);
@ -26,12 +26,12 @@ void Server::begin()
}
}
void Server::accept()
void EthernetServer::accept()
{
int listening = 0;
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock);
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port) {
if (client.status() == SnSR::LISTEN) {
@ -48,12 +48,12 @@ void Server::accept()
}
}
Client Server::available()
EthernetClient EthernetServer::available()
{
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock);
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
(client.status() == SnSR::ESTABLISHED ||
client.status() == SnSR::CLOSE_WAIT)) {
@ -64,27 +64,27 @@ Client Server::available()
}
}
return Client(MAX_SOCK_NUM);
return EthernetClient(MAX_SOCK_NUM);
}
size_t Server::write(uint8_t b)
size_t EthernetServer::write(uint8_t b)
{
write(&b, 1);
}
size_t Server::write(const char *str)
size_t EthernetServer::write(const char *str)
{
write((const uint8_t *)str, strlen(str));
}
size_t Server::write(const uint8_t *buffer, size_t size)
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
Client client(sock);
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) {

View File

@ -1,18 +1,18 @@
#ifndef server_h
#define server_h
#ifndef ethernetserver_h
#define ethernetserver_h
#include "NetServer.h"
#include "Server.h"
class Client;
class EthernetClient;
class Server :
public NetServer {
class EthernetServer :
public Server {
private:
uint16_t _port;
void accept();
public:
Server(uint16_t);
Client available();
EthernetServer(uint16_t);
EthernetClient available();
virtual void begin();
virtual size_t write(uint8_t);
virtual size_t write(const char *str);

View File

@ -39,7 +39,7 @@ IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
EthernetServer server(80);
//Sensor's memory register addresses:
@ -96,7 +96,7 @@ void loop() {
}
// listen for incoming Ethernet connections:
listenForClients();
listenForEthernetClients();
}
@ -124,9 +124,9 @@ void getData() {
Serial.println(" Pa");
}
void listenForClients() {
void listenForEthernetClients() {
// listen for incoming clients
Client client = server.available();
EthernetClient client = server.available();
if (client) {
Serial.println("Got a client");
// an http request ends with a blank line

View File

@ -29,7 +29,7 @@ IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
Server server(23);
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
@ -43,7 +43,7 @@ void setup() {
void loop() {
// wait for a new client:
Client client = server.available();
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {

View File

@ -24,7 +24,7 @@ byte mac[] = {
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
EthernetClient client;
void setup() {
// start the serial library:

View File

@ -30,7 +30,7 @@ IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
Server server(23);
EthernetServer server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
@ -59,7 +59,7 @@ void setup() {
void loop() {
// wait for a new client:
Client client = server.available();
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {

View File

@ -25,7 +25,7 @@ char serverName[] = "www.google.com";
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
EthernetClient client;
void setup() {
// start the serial library:

View File

@ -29,7 +29,7 @@ byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance:
Client client;
EthernetClient client;
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop

View File

@ -30,7 +30,7 @@ byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance:
Client client;
EthernetClient client;
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop

View File

@ -33,7 +33,7 @@ IPAddress server(1,1,1,1);
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use port 10002):
Client client;
EthernetClient client;
void setup() {
// start the Ethernet connection:

View File

@ -33,7 +33,7 @@ byte mac[] = {
IPAddress ip(192,168,1,20);
// initialize the library instance:
Client client;
EthernetClient client;
const int requestInterval = 60000; // delay between requests

View File

@ -23,7 +23,7 @@ IPAddress server(173,194,33,104); // Google
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
EthernetClient client;
void setup() {
// start the serial library:

View File

@ -1,5 +1,5 @@
/*
Web Server
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
@ -26,7 +26,7 @@ IPAddress ip(192,168,1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
EthernetServer server(80);
void setup()
{
@ -38,7 +38,7 @@ void setup()
void loop()
{
// listen for incoming clients
Client client = server.available();
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;

View File

@ -7,8 +7,8 @@
#######################################
Ethernet KEYWORD1
Client KEYWORD1
Server KEYWORD1
EthernetClient KEYWORD1
EthernetServer KEYWORD1
IPAddress KEYWORD1
#######################################