Takuma Tachibana / CushionSock

Files at this revision

API Documentation at this revision

Comitter:
3116redtrain
Date:
Sat Dec 17 09:13:37 2016 +0000
Child:
1:e6addce35f93
Commit message:
first issue

Changed in this revision

CushionSock.cpp Show annotated file Show diff for this revision Revisions of this file
CushionSock.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CushionSock.cpp	Sat Dec 17 09:13:37 2016 +0000
@@ -0,0 +1,86 @@
+#include "CushionSock.h"
+
+CushionSock::CushionSock(char * hostAndPort){
+    eth = new EthernetNetIf();
+    EthernetErr ethErr = eth->setup();
+    if (ethErr) {
+        printf("\r\nERROR %d in setup.\r\n", ethErr);
+    }
+    printf("hostandport %s\n", hostAndPort);
+    
+    ws = new Websocket(hostAndPort, eth);
+
+    printf("begin\r\n");
+
+    MESS_STOP = 0x01;
+    MESS_START = 0x02;
+    MESS_SIT = 0x11;
+    MESS_FRUST = 0x12;
+    MESS_TEST =0x21;
+    type_name[MESS_STOP]="stop";
+    type_name[MESS_START]="start";
+    type_name[MESS_SIT]="status_sit";
+    type_name[MESS_FRUST]="status_frust";
+    type_name[MESS_TEST]="test";
+    name_type["stop"]=MESS_STOP;
+    name_type["start"]=MESS_START;
+    name_type["status_sit"]=MESS_SIT;
+    name_type["status_frust"]=MESS_FRUST;
+    name_type["test"]=MESS_TEST;
+    
+    printf("websock init\n");
+     int failcount=0;
+     
+     while(! ws->connect()) {
+        failcount++;
+        if(failcount>10){
+            printf("cannot connect failed.\n");
+            break;
+        }
+        printf("cannot connect websocket, retrying...\n");
+        wait(2);
+    }
+}
+
+    
+bool CushionSock::connect(){
+     
+     return true;
+}
+  
+int CushionSock::mess_send(int type, char * msg){
+    string name=type_name[type];
+    MbedJSONValue json;
+    json["name"]=name.c_str();
+    json["value"]=msg;
+    ws->send((char*)json.serialize().c_str());
+    return 1;
+} 
+    
+int CushionSock::mess_recv(char * msg){
+    picojson::value v;
+    char recv[256];
+    if (ws->read(recv)) {
+        int type;
+        string name;
+        string sbuf(recv);
+        const char * crecv=sbuf.c_str();
+        string err = picojson::parse(v,crecv,crecv+strlen(crecv));
+        if(!err.empty())
+            return -1;
+        name=v.get("name").get<string>();
+        strncpy(msg,v.get("value").get<string>().c_str(),256);
+        type=name_type[name];
+        return type;
+    }else{
+        return -1;
+    }
+}
+ 
+bool CushionSock::is_connected(){
+    return ws->connected();
+}
+
+bool CushionSock::close(){
+    return ws->close();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CushionSock.h	Sat Dec 17 09:13:37 2016 +0000
@@ -0,0 +1,62 @@
+#ifndef CUSHION_SOCK_H
+#define CUSHION_SOCK_H
+
+#include "mbed.h"
+#include "Websocket.h"
+#include "EthernetNetIf.h"
+#include "picojson.h"
+#include "MbedJSONValue.h"
+
+
+class CushionSock
+{
+    public:
+    int MESS_STOP;
+    int MESS_START;
+    int MESS_SIT;
+    int MESS_FRUST;
+    int MESS_TEST;
+    
+    /*
+        Constructor
+        @param hostAndport format : "hostname:port"        
+    */
+    CushionSock(char * hostAndPort);  
+    
+    /*
+       connect to server
+       @return true if the connection is established, false otherwise
+    */
+    bool connect();
+    
+    /*
+       send message to server
+       @return the number of bytes sent 
+       @param type Message type(prease show and choose from line 14 to 17 in souece code)
+       @param msg message payload  
+    */    
+    int mess_send(int type, char * msg);
+    
+    /*
+        Recieve message from server
+        @return if recieved succsessful, Message type(prease show and choose from line 14 to 17 in souece code), otherwise returns -1.
+        @param msg pointer to the message to be read.
+    */
+    int mess_recv(char * msg);
+    
+    /*
+     To see if there is a SocketIO connection active
+     @return true if there is a connection active
+    */
+    bool is_connected();
+    
+    bool close();
+    
+    private:
+    EthernetNetIf *eth;
+    Websocket *ws;
+
+    std::map<int,string> type_name;
+    std::map<string,int> name_type;
+};
+#endif
\ No newline at end of file