blockchain , sdchain cpp sdk and demo

Dependencies:   EthernetInterface mbed-rtos mbed uniqueCPUID

Fork of bcsdk by SDchain C Plus Plus Team

Revision:
8:f2a567ee3a46
Parent:
7:04744a9ce2b9
Child:
9:d7468574ef8f
--- a/main.cpp	Sun Apr 12 03:04:28 2015 +0000
+++ b/main.cpp	Thu Sep 21 02:09:04 2017 +0000
@@ -2,7 +2,15 @@
 #include "EthernetInterface.h"
 #include "SDFileSystem.h"
 #include <stdio.h>
-#include <string.h>
+//#include <string.h>
+
+#include <iostream>  
+#include <string>  
+#include "UnitTest.h"
+//#include "BlockChain.h"
+#include "MockHsm.h"
+
+using namespace std;  
 
 #define HTTPD_SERVER_PORT   80
 #define HTTPD_MAX_REQ_LENGTH   1023
@@ -10,6 +18,9 @@
 #define HTTPD_MAX_FNAME_LENGTH   127
 #define HTTPD_MAX_DNAME_LENGTH   127
 
+const char* ECHO_SERVER_ADDRESS = "192.168.31.185";
+const int ECHO_SERVER_PORT = 10003;
+
 Serial uart(USBTX, USBRX);
 
 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
@@ -19,6 +30,7 @@
 EthernetInterface eth;
 TCPSocketServer server;
 TCPSocketConnection client;
+TCPSocketConnection my_client;
 
 char buffer[HTTPD_MAX_REQ_LENGTH+1];
 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
@@ -31,6 +43,82 @@
 FILE *fp;
 int rdCnt;
 
+// function declation
+int test_socket_client(void);
+int socket_init();
+int socket_fini();
+
+int test_MockHsm()
+{
+    MockHsm mh;
+    mh.setAlias("cpp_key");
+    //cout<<mh.getAlias()<<endl;
+    //cout<<mh.createKey()<<endl;
+    cout<< mh.listKeys() <<endl;
+    return 0;
+}
+
+int main(void)
+{
+    printf("main start\n");
+    
+    socket_init();
+    
+    test_MockHsm();
+    
+    test_socket_client();
+    
+    socket_fini();
+    
+    printf("main end\n");
+    return 0;
+}
+
+int socket_init()
+{
+    int ret = 0;
+    // EthernetInterface eth;
+    printf("Initializing Ethernet\n");
+    ret = eth.init(); //Use DHCP
+    //eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use static ip
+    printf("Connecting\n");
+    ret = eth.connect();
+    printf("IP Address is %s\n", eth.getIPAddress());
+    
+    return ret;
+}
+
+int socket_fini()
+{
+    return eth.disconnect();  
+}
+
+int test_socket_client(void)
+{
+    TCPSocketConnection socket;
+    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+        wait(1);
+    }
+    printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+    
+    // Send message to server
+    char hello[] = "Hello World";
+    printf("Sending  message to Server : '%s' \n",hello);
+    socket.send_all(hello, sizeof(hello) - 1);
+    
+    // Receive message from server
+    char buf[256];
+    int n = socket.receive(buf, 256);
+    buf[n] = '\0';
+    printf("Received message from server: '%s'\n", buf);
+    
+    // Clean up
+    socket.close();
+    
+    return 0;
+}
+
 void get_file(char* uri)
 {
     uart.printf("get_file %s\n", uri);
@@ -86,7 +174,7 @@
     }
 }
 
-int main (void)
+int main_1 (void)
 {
 //    Serial Interface eth;
     uart.baud(115200);
@@ -103,10 +191,37 @@
 
 //    EthernetInterface eth;
     uart.printf("Initializing Ethernet\n");
-    eth.init(); //Use DHCP
+    //eth.init(); //Use DHCP
+    eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use  
     uart.printf("Connecting\n");
     eth.connect();
     uart.printf("IP Address is %s\n", eth.getIPAddress());
+    
+    //test socket client
+    //my_client.connect("192.168.31.185",10003);
+    //my_client.send_all("hello world",11);
+    
+     TCPSocketConnection socket;
+    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+        uart.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+        wait(1);
+    }
+    uart.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+    
+    // Send message to server
+    char hello[] = "Hello World";
+    uart.printf("Sending  message to Server : '%s' \n",hello);
+    socket.send_all(hello, sizeof(hello) - 1);
+    
+    // Receive message from server
+    char buf[256];
+    int n = socket.receive(buf, 256);
+    buf[n] = '\0';
+    uart.printf("Received message from server: '%s'\n", buf);
+    
+    // Clean up
+    socket.close();
+    ////////////////////////////////////////////////////////////////////////////
 
 //    TCPSocketServer server;
     server.bind(HTTPD_SERVER_PORT);
@@ -117,6 +232,8 @@
         uart.printf("\nWait for new connection...\r\n");
         server.accept(client);
         client.set_blocking(false, 1500); // Timeout after (1.5)s
+        
+        my_client.send_all("hello world",11);
 
         uart.printf("Connection from: %s\r\n", client.get_address());
         while (true) {
@@ -148,3 +265,40 @@
         client.close();
     }
 }
+
+
+int main_2() {
+    //    Serial Interface eth;
+    uart.baud(115200);
+    uart.printf("Initializing\n");
+    
+    EthernetInterface eth;
+    eth.init(); //Use DHCP
+    eth.connect();
+    printf("\nClient IP Address is %s\n", eth.getIPAddress());
+    
+    // Connect to Server
+    TCPSocketConnection socket;
+    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+        wait(1);
+    }
+    printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+    
+    // Send message to server
+    char hello[] = "Hello World";
+    printf("Sending  message to Server : '%s' \n",hello);
+    socket.send_all(hello, sizeof(hello) - 1);
+    
+    // Receive message from server
+    char buf[256];
+    int n = socket.receive(buf, 256);
+    buf[n] = '\0';
+    printf("Received message from server: '%s'\n", buf);
+    
+    // Clean up
+    socket.close();
+    eth.disconnect();
+    
+    while(true) {}
+}
\ No newline at end of file