Skip to content

Commit

Permalink
Changing inheritance of WiFlyClient from Stream to Client
Browse files Browse the repository at this point in the history
	API Change: WiFlyClient Constructor no longer takes ip,port
	API Change: WiFlyClient connect now takes ip/domain, port (match's Client.h implementation)
	Updated examples to reflect change
	Minor tweak to WiFlyServer
  • Loading branch information
dpslwk authored and jcrouchley committed Apr 4, 2012
1 parent 586f8ab commit 66254e3
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 73 deletions.
70 changes: 36 additions & 34 deletions WiFlyClient.cpp
Expand Up @@ -2,59 +2,51 @@
#include "WiFly.h" #include "WiFly.h"
#include "WiFlyClient.h" #include "WiFlyClient.h"


WiFlyClient::WiFlyClient(uint8_t *ip, uint16_t port) : WiFlyClient::WiFlyClient() :
_WiFly (WiFly) { _WiFly (WiFly) {
//stream (ParsedStream(SpiSerial)) {
// TODO: Find out why neither of the following work as expected.
// (The result of `read()` is always -1. )
//stream (ParsedStream(_WiFly.uart)) {
//stream (ParsedStream(WiFly.uart)) {
/*
*/

_ip = ip;
_port = port;
_domain = NULL;

isOpen = false;
}


WiFlyClient::WiFlyClient(const char* domain, uint16_t port) :
_WiFly (WiFly) {
//stream (ParsedStream(SpiSerial)) {
// TODO: Find out why neither of the following work as expected.
// (The result of `read()` is always -1. )
//stream (ParsedStream(_WiFly.uart)) {
//stream (ParsedStream(WiFly.uart)) {
/*
*/
_ip = NULL;
_port = port;
_domain = domain;


isOpen = false; isOpen = false;
} }




size_t WiFlyClient::write(byte value) { size_t WiFlyClient::write(uint8_t value) {
/* /*
*/ */
_WiFly.uart->write(value); _WiFly.uart->write(value);
return 1; return 1;
} }




size_t WiFlyClient::write(const uint8_t *buffer, size_t size) { size_t WiFlyClient::write(const uint8_t *buf, size_t size) {
/* /*
*/ */
while(size--) while(size--)
_WiFly.uart->write(*buffer++); _WiFly.uart->write(*buf++);
return size; return size;
} }


int WiFlyClient::connect(IPAddress ip, uint16_t port){
/*
*/

_ip = rawIPAddress(ip);
_port = port;
_domain = NULL;

return _connect();
}


boolean WiFlyClient::connect() { int WiFlyClient::connect(const char *host, uint16_t port){
/*
*/
_ip = NULL;
_port = port;
_domain = host;

return _connect();
}

boolean WiFlyClient::_connect() {
/* /*
*/ */


Expand Down Expand Up @@ -127,6 +119,16 @@ int WiFlyClient::read() {
return stream.read(); return stream.read();
} }


int WiFlyClient::read(uint8_t *buf, size_t size){
/*
*/
if (!isOpen) {
return -1;
}
// ***LWK*** TODO make this work
return stream.read();
}

int WiFlyClient::peek() { int WiFlyClient::peek() {
if (!isOpen) { if (!isOpen) {
return -1; return -1;
Expand All @@ -147,7 +149,7 @@ void WiFlyClient::flush(void) {
} }




bool WiFlyClient::connected() { uint8_t WiFlyClient::connected() {
/* /*
*/ */
// TODO: Set isOpen to false once we know the stream is closed? // TODO: Set isOpen to false once we know the stream is closed?
Expand Down
43 changes: 20 additions & 23 deletions WiFlyClient.h
Expand Up @@ -4,41 +4,38 @@
#ifndef __WIFLY_CLIENT_H__ #ifndef __WIFLY_CLIENT_H__
#define __WIFLY_CLIENT_H__ #define __WIFLY_CLIENT_H__


#include "Stream.h" #include "Client.h"
#include "IPAddress.h"


#include "ParsedStream.h" #include "ParsedStream.h"


#include "WiFlyDevice.h" #include "WiFlyDevice.h"


class WiFlyClient : public Stream { class WiFlyClient : public Client {
public: public:
WiFlyClient(uint8_t *ip, uint16_t port); WiFlyClient();
WiFlyClient(const char* domain, uint16_t port);


boolean connect(); virtual int connect(IPAddress ip, uint16_t port);

virtual int connect(const char *host, uint16_t port);
size_t write(byte value); virtual size_t write(uint8_t);
size_t write(const char *str); virtual size_t write(const uint8_t *buf, size_t size);
size_t write(const uint8_t *buffer, size_t size); virtual int available();

virtual int read();
int available(); virtual int read(uint8_t *buf, size_t size);
int read(); virtual int peek();
void flush(void); virtual void flush();
int peek(); virtual void stop();

virtual uint8_t connected();
bool connected(); virtual operator bool();
void stop();

operator bool();



uint8_t *_ip; uint8_t *_ip;
uint16_t _port; uint16_t _port;


const char *_domain; const char *_domain;
private: private:
WiFlyDevice& _WiFly; WiFlyDevice& _WiFly;

boolean _connect();


bool isOpen; bool isOpen;


Expand All @@ -47,7 +44,7 @@ class WiFlyClient : public Stream {


// TODO: Work out why alternate instantiation code in // TODO: Work out why alternate instantiation code in
// Server.available() doesn't work and thus requires this: // Server.available() doesn't work and thus requires this:
friend class Server; friend class WiFlyServer;
}; };


#endif #endif
8 changes: 2 additions & 6 deletions WiFlyServer.cpp
@@ -1,11 +1,7 @@
#include "WiFly.h" #include "WiFly.h"


// NOTE: Arbitrary cast to avoid constructor ambiguity.
// TODO: Handle this a different way so we're not using
// NULL pointers all over the place?
#define NO_CLIENT WiFlyClient ((uint8_t*) NULL, 0)


WiFlyServer::WiFlyServer(uint16_t port) : activeClient(NO_CLIENT){ WiFlyServer::WiFlyServer(uint16_t port) : activeClient(){
/* /*
*/ */
_port = port; _port = port;
Expand Down Expand Up @@ -50,7 +46,7 @@ WiFlyClient& WiFlyServer::available() {
activeClient._domain = NULL; activeClient._domain = NULL;
activeClient._ip = NULL; activeClient._ip = NULL;


activeClient.connect(); activeClient._connect();
WiFly.serverConnectionActive = true; WiFly.serverConnectionActive = true;
} else { } else {
// Ignore other feedback from the WiFly module. // Ignore other feedback from the WiFly module.
Expand Down
4 changes: 2 additions & 2 deletions WiFlyServer.h
@@ -1,5 +1,5 @@
#ifndef __SERVER_H__ #ifndef __WIFLY_SERVER_H__
#define __SERVER_H__ #define __WIFLY_SERVER_H__


#include <stdint.h> #include <stdint.h>


Expand Down
4 changes: 2 additions & 2 deletions examples/WiFly_PachubeClient/WiFly_PachubeClient.ino
Expand Up @@ -24,7 +24,7 @@ SoftwareSerial mySerial(2, 3);
#include "Credentials.h" #include "Credentials.h"


// Using Pachube API V2 // Using Pachube API V2
WiFlyClient client("api.pachube.com", 80); WiFlyClient client;


void setup() { void setup() {


Expand Down Expand Up @@ -69,7 +69,7 @@ void loop() {
// feedID can be the datastream name of the numberic ID // feedID can be the datastream name of the numberic ID
sprintf(buff,"0,%d\n1,%d",i++,analogRead(0)); sprintf(buff,"0,%d\n1,%d",i++,analogRead(0));
Serial.println("connecting..."); Serial.println("connecting...");
if (client.connect()) { if (client.connect("api.pachube.com", 80)) {
Serial.println("connected"); Serial.println("connected");
client.print("PUT /v2/feeds/"); // APIV2 client.print("PUT /v2/feeds/"); // APIV2
client.print(PACHUBEFEED); client.print(PACHUBEFEED);
Expand Down
Expand Up @@ -24,7 +24,7 @@ SoftwareSerial mySerial(2, 3);
#include "Credentials.h" #include "Credentials.h"


// Using Pachube API V2 // Using Pachube API V2
WiFlyClient client("api.pachube.com", 80); WiFlyClient client;


void setup() { void setup() {


Expand Down Expand Up @@ -69,7 +69,7 @@ void loop() {
// feedID can be the datastream name of the numberic ID // feedID can be the datastream name of the numberic ID
sprintf(buff,"0,%d\n1,%d",i++,analogRead(0)); sprintf(buff,"0,%d\n1,%d",i++,analogRead(0));
mySerial.println("connecting..."); mySerial.println("connecting...");
if (client.connect()) { if (client.connect("api.pachube.com", 80)) {
mySerial.println("connected"); mySerial.println("connected");
client.print("PUT /v2/feeds/"); // APIV2 client.print("PUT /v2/feeds/"); // APIV2
client.print(PACHUBEFEED); client.print(PACHUBEFEED);
Expand Down
4 changes: 2 additions & 2 deletions examples/WiFly_WebClient/WiFly_WebClient.ino
Expand Up @@ -11,7 +11,7 @@ byte server[] = { 66, 249, 89, 104 }; // Google


//Client client(server, 80); //Client client(server, 80);


WiFlyClient client("google.com", 80); WiFlyClient client;


void setup() { void setup() {


Expand All @@ -28,7 +28,7 @@ void setup() {


Serial.println("connecting..."); Serial.println("connecting...");


if (client.connect()) { if (client.connect("google.com", 80)) {
Serial.println("connected"); Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0"); client.println("GET /search?q=arduino HTTP/1.0");
client.println(); client.println();
Expand Down
4 changes: 2 additions & 2 deletions examples/WiFly_WebClient_Faster/WiFly_WebClient_Faster.ino
Expand Up @@ -11,7 +11,7 @@ byte server[] = { 66, 249, 89, 104 }; // Google


//Client client(server, 80); //Client client(server, 80);


WiFlyClient client("google.com", 80); WiFlyClient client;


void setup() { void setup() {


Expand All @@ -31,7 +31,7 @@ void setup() {


Serial.println("connecting..."); Serial.println("connecting...");


if (client.connect()) { if (client.connect("google.com", 80)) {
Serial.println("connected"); Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0"); client.println("GET /search?q=arduino HTTP/1.0");
client.println(); client.println();
Expand Down

0 comments on commit 66254e3

Please sign in to comment.