<file_sep># android-sensor-feed
Cloud Computing Assignment - Web Sockets  
"<NAME>" University of Iași, Romania  
Faculty of Computer Science  

<NAME>  
<EMAIL>

## Description
This is a minimal client-server application used to receive sensor readings from android devices via web sockets.

## Observations
* Just like my [Python3 REST Api](https://github.com/Bogdan-Stefan/python3-rest-api), this project has been created for educational purposes. It's not perfect and I'm learning from my mistakes.
* The server is written in python3 using [CherryPy](http://cherrypy.org/) and [ws4py](https://github.com/Lawouach/WebSocket-for-Python).
* The client is a native android app. The only external library used is [Java-WebSocket](https://github.com/TooTallNate/Java-WebSocket).
* Server supports multiple connected clients.

## Useful Links
[websocket.org](http://websocket.org/)  
[What are web sockets?](https://pusher.com/websockets)  
[WebSocket - Why, what, and - can I use it?](http://crossbario.com/blog/Websocket-Why-What-Can-I-Use-It/)  
<file_sep>package com.example.bogdan_stefan.sensorfeed;


interface WebSocketEventHandler {
    void connectionOpened();

    void connectionClosed();

    void exceptionRaised(String errorMessage, Exception e);
}
<file_sep>package com.example.bogdan_stefan.sensorfeed;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;


class SensorFeedClient extends WebSocketClient {
    private WebSocketEventHandler webSocketEventHandler;

    SensorFeedClient(URI serverUri, WebSocketEventHandler webSocketEventHandler) {
        super(serverUri);
        this.webSocketEventHandler = webSocketEventHandler;
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        webSocketEventHandler.connectionOpened();
    }

    @Override
    public void onMessage(String s) {
        System.out.println("Received: " + s);
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        webSocketEventHandler.connectionClosed();
    }

    @Override
    public void onError(Exception e) {
        e.printStackTrace();
        webSocketEventHandler.exceptionRaised("Web Socket Error!", e);
    }
}
<file_sep>import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket

cherrypy.server.socket_host = 'server-ip'
cherrypy.config.update({'server.socket_port': 9000})
WebSocketPlugin(cherrypy.engine).subscribe()
cherrypy.tools.websocket = WebSocketTool()


class FeedPrinter(WebSocket):
    def received_message(self, message):
        """Prints the values received from client."""
        values = str(message).split(',')
        print(values[0])
        print("X Axis -> ", values[1])
        print("Y Axis -> ", values[2])
        print("Z Axis -> ", values[3])
        print("\n")


class Root(object):
    @cherrypy.expose
    def index(self):
        return 'some HTML with a websocket javascript connection'

    @cherrypy.expose
    def ws(self):
        # you can access the class instance through
        handler = cherrypy.request.ws_handler


cherrypy.quickstart(Root(), '/', config={'/ws': {'tools.websocket.on': True,
                                                 'tools.websocket.handler_cls': FeedPrinter}})
