1 /* 2 * LwsService.java - libwebsockets test service for Android 3 * 4 * Copyright (C) 2016 Alexander Bruines <alexander.bruines@gmail.com> 5 * 6 * This file is made available under the Creative Commons CC0 1.0 7 * Universal Public Domain Dedication. 8 * 9 * The person who associated a work with this deed has dedicated 10 * the work to the public domain by waiving all of his or her rights 11 * to the work worldwide under copyright law, including all related 12 * and neighboring rights, to the extent allowed by law. You can copy, 13 * modify, distribute and perform the work, even for commercial purposes, 14 * all without asking permission. 15 * 16 * The test apps are intended to be adapted for use in your code, which 17 * may be proprietary. So unlike the library itself, they are licensed 18 * Public Domain. 19 */ 20 21 package org.libwebsockets.client; 22 23 import android.os.Message; 24 import android.os.RemoteException; 25 import android.util.Log; 26 27 public class LwsService extends ThreadService { 28 29 /** 30 * Commands that can be send to this service 31 */ 32 public final static int MSG_SET_CONNECTION_PARAMETERS = 1; 33 34 /** 35 * Messages that may be send to output Messenger 36 * Clients should handle these messages. 37 **/ 38 public final static int MSG_DUMB_INCREMENT_PROTOCOL_COUNTER = 1; 39 public final static int MSG_LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 2; 40 public final static int MSG_LWS_CALLBACK_CLIENT_ESTABLISHED = 3; 41 42 public static class ConnectionParameters { 43 String serverAddress; 44 int serverPort; 45 ConnectionParameters( String serverAddress, int serverPort )46 ConnectionParameters( 47 String serverAddress, 48 int serverPort 49 ){ 50 this.serverAddress = serverAddress; 51 this.serverPort = serverPort; 52 } 53 } 54 55 /** 56 * Handle incoming messages from clients of this service 57 */ 58 @Override handleInputMessage(Message msg)59 public void handleInputMessage(Message msg) { 60 Message m; 61 switch(msg.what) { 62 case MSG_SET_CONNECTION_PARAMETERS: { 63 LwsService.ConnectionParameters parameters = (ConnectionParameters) msg.obj; 64 setConnectionParameters( 65 parameters.serverAddress, 66 parameters.serverPort 67 ); 68 break; 69 } 70 default: 71 super.handleInputMessage(msg); 72 break; 73 } 74 } 75 76 /** 77 * The run() function for the thread. 78 * For this test we implement a very long lived task 79 * that sends many messages back to the client. 80 * **/ workerThreadRun()81 public void workerThreadRun() { 82 83 initLws(); 84 connectLws(); 85 86 while(true) { 87 88 // service the websockets 89 serviceLws(); 90 91 // Check if we must quit or suspend 92 synchronized (mThreadLock){ 93 while(mMustSuspend) { 94 // We are asked to suspend the thread 95 try { 96 mThreadLock.wait(); 97 98 } catch (InterruptedException e) {} 99 } 100 if(mMustQuit) { 101 // The signal to quit was given 102 break; 103 } 104 } 105 106 // Throttle the loop so that it iterates once every 50ms 107 try { 108 Thread.sleep(50); 109 } 110 catch (InterruptedException e) { 111 e.printStackTrace(); 112 } 113 114 } 115 exitLws(); 116 } 117 118 /** Load the native libwebsockets code */ 119 static { 120 try { 121 System.loadLibrary("lwsservice"); 122 } 123 catch(UnsatisfiedLinkError ule) { 124 Log.e("LwsService", "Warning: Could not load native library: " + ule.getMessage()); 125 } 126 } initLws()127 public native boolean initLws(); exitLws()128 public native void exitLws(); serviceLws()129 public native void serviceLws(); setConnectionParameters(String serverAddress, int serverPort)130 public native void setConnectionParameters(String serverAddress, int serverPort); connectLws()131 public native boolean connectLws(); 132 } 133