Permalink
Browse files

Changing inheritance of WiFlyClient from Stream to Client

	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...
1 parent 586f8ab commit 66254e3e624b105595d2b77a6c1c1ff248841648 @dpslwk dpslwk committed with Mar 13, 2012
View
@@ -2,59 +2,51 @@
#include "WiFly.h"
#include "WiFlyClient.h"
-WiFlyClient::WiFlyClient(uint8_t *ip, uint16_t port) :
+WiFlyClient::WiFlyClient() :
_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;
}
-size_t WiFlyClient::write(byte value) {
+size_t WiFlyClient::write(uint8_t value) {
/*
*/
_WiFly.uart->write(value);
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--)
- _WiFly.uart->write(*buffer++);
+ _WiFly.uart->write(*buf++);
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() {
/*
*/
@@ -127,6 +119,16 @@ int WiFlyClient::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() {
if (!isOpen) {
return -1;
@@ -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?
View
@@ -4,41 +4,38 @@
#ifndef __WIFLY_CLIENT_H__
#define __WIFLY_CLIENT_H__
-#include "Stream.h"
+#include "Client.h"
+#include "IPAddress.h"
#include "ParsedStream.h"
#include "WiFlyDevice.h"
-class WiFlyClient : public Stream {
+class WiFlyClient : public Client {
public:
- WiFlyClient(uint8_t *ip, uint16_t port);
- WiFlyClient(const char* domain, uint16_t port);
-
- boolean connect();
-
- size_t write(byte value);
- size_t write(const char *str);
- size_t write(const uint8_t *buffer, size_t size);
-
- int available();
- int read();
- void flush(void);
- int peek();
-
- bool connected();
- void stop();
-
- operator bool();
-
+ WiFlyClient();
+
+
+ virtual int connect(IPAddress ip, uint16_t port);
+ virtual int connect(const char *host, uint16_t port);
+ virtual size_t write(uint8_t);
+ virtual size_t write(const uint8_t *buf, size_t size);
+ virtual int available();
+ virtual int read();
+ virtual int read(uint8_t *buf, size_t size);
+ virtual int peek();
+ virtual void flush();
+ virtual void stop();
+ virtual uint8_t connected();
+ virtual operator bool();
uint8_t *_ip;
uint16_t _port;
const char *_domain;
private:
WiFlyDevice& _WiFly;
-
+ boolean _connect();
bool isOpen;
@@ -47,7 +44,7 @@ class WiFlyClient : public Stream {
// TODO: Work out why alternate instantiation code in
// Server.available() doesn't work and thus requires this:
- friend class Server;
+ friend class WiFlyServer;
};
#endif
View
@@ -1,11 +1,7 @@
#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;
@@ -50,7 +46,7 @@ WiFlyClient& WiFlyServer::available() {
activeClient._domain = NULL;
activeClient._ip = NULL;
- activeClient.connect();
+ activeClient._connect();
WiFly.serverConnectionActive = true;
} else {
// Ignore other feedback from the WiFly module.
View
@@ -1,5 +1,5 @@
-#ifndef __SERVER_H__
-#define __SERVER_H__
+#ifndef __WIFLY_SERVER_H__
+#define __WIFLY_SERVER_H__
#include <stdint.h>
@@ -24,7 +24,7 @@ SoftwareSerial mySerial(2, 3);
#include "Credentials.h"
// Using Pachube API V2
-WiFlyClient client("api.pachube.com", 80);
+WiFlyClient client;
void setup() {
@@ -69,7 +69,7 @@ void loop() {
// feedID can be the datastream name of the numberic ID
sprintf(buff,"0,%d\n1,%d",i++,analogRead(0));
Serial.println("connecting...");
- if (client.connect()) {
+ if (client.connect("api.pachube.com", 80)) {
Serial.println("connected");
client.print("PUT /v2/feeds/"); // APIV2
client.print(PACHUBEFEED);
@@ -24,7 +24,7 @@ SoftwareSerial mySerial(2, 3);
#include "Credentials.h"
// Using Pachube API V2
-WiFlyClient client("api.pachube.com", 80);
+WiFlyClient client;
void setup() {
@@ -69,7 +69,7 @@ void loop() {
// feedID can be the datastream name of the numberic ID
sprintf(buff,"0,%d\n1,%d",i++,analogRead(0));
mySerial.println("connecting...");
- if (client.connect()) {
+ if (client.connect("api.pachube.com", 80)) {
mySerial.println("connected");
client.print("PUT /v2/feeds/"); // APIV2
client.print(PACHUBEFEED);
@@ -11,7 +11,7 @@ byte server[] = { 66, 249, 89, 104 }; // Google
//Client client(server, 80);
-WiFlyClient client("google.com", 80);
+WiFlyClient client;
void setup() {
@@ -28,7 +28,7 @@ void setup() {
Serial.println("connecting...");
- if (client.connect()) {
+ if (client.connect("google.com", 80)) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
@@ -11,7 +11,7 @@ byte server[] = { 66, 249, 89, 104 }; // Google
//Client client(server, 80);
-WiFlyClient client("google.com", 80);
+WiFlyClient client;
void setup() {
@@ -31,7 +31,7 @@ void setup() {
Serial.println("connecting...");
- if (client.connect()) {
+ if (client.connect("google.com", 80)) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();

0 comments on commit 66254e3

Please sign in to comment.