Android Open Source - ubisoldiers Uos Manager






From Project

Back to project page ubisoldiers.

License

The source code is released under:

MIT License

If you think the Android project ubisoldiers listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.gobbisanches.ubisoldiers.app;
//www. j a v  a 2 s  .c  om
import android.content.Context;
import android.location.Location;
import android.os.AsyncTask;
import android.util.Log;
import org.unbiquitous.uos.core.UOS;
import org.unbiquitous.uos.core.adaptabitilyEngine.Gateway;
import org.unbiquitous.uos.core.adaptabitilyEngine.SmartSpaceGateway;
import org.unbiquitous.uos.core.driverManager.DriverData;
import org.unbiquitous.uos.core.driverManager.UosDriver;
import org.unbiquitous.uos.core.messageEngine.dataType.UpDriver;
import org.unbiquitous.uos.core.driverManager.DriverManager;
import org.unbiquitous.uos.core.messageEngine.messages.ServiceResponse;

import java.io.IOException;
import java.io.InputStream;

import java.util.*;

/**
 * Created by Sanches on 13/07/2014.
 */

// UosManager runs in the main task, but its inner tasks run on another threads
public class UosManager {
    private static UosManager instance = null;
    private Context context;
    private UOS uos;

    class StartUosTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                uos = new UOS();
                uos.init(getResourceBundleFromFile());
                Log.i("UBISOLDIERS", "UOS was successfully initialized");
            } catch(Exception e) {
                Log.e("UBISOLDIERS", "Could not initiliaze UOS, because: " + e.getMessage());
            }

            return null;
        }

        private ResourceBundle getResourceBundleFromFile() {
//            InputStream is;
//            final ResourceBundle bundle;
//
//            try {
//                is = context.getAssets().open("ubiquitous.properties");
//                bundle = new PropertyResourceBundle(is);
//            } catch (IOException e) {
//                throw new RuntimeException("Could not open UOS bundle file: " + e.getMessage());
//            }
//
//            return bundle;

            return new ListResourceBundle() {
                @Override
                protected Object[][] getContents() {
                    return new Object[][]{
//                            {"ubiquitos.application.deploylist", UbisoldierUosApplication.class.getName()},
                            {"ubiquitos.connectionManager", "org.unbiquitous.uos.core.network.loopback.connectionManager.LoopbackConnectionManager"},
//                            {"ubiquitos.driver.deploylist", UbisoldiersDriver.class.getName()},
                            {"ubiquitos.driver.path", "/"},
                    };
                }
            };
        }
    }

    public class SearchParameters {
        private long randomSeed;
        private double modifier;

        public SearchParameters(long randomSeed, double modifier) {
            this.randomSeed = randomSeed;
            this.modifier = modifier;
        }

        public long getRandomSeed() {
            return randomSeed;
        }

        public double getModifier() {
            return modifier;
        }
    }

    UosManager(Context context) {
        this.context = context;
    }

    void startUos() {
        new StartUosTask().execute();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(uos == null) {
            throw new RuntimeException("Invalid UOS object");
        }

        if(uos.getFactory() == null) {
            throw new RuntimeException("Invalid UOS Factory");
        }

        SmartSpaceGateway gateway = (SmartSpaceGateway) uos.getGateway();
        if(gateway == null) {
            throw new RuntimeException("Invalid UOS Gateway");
        }

        DriverManager driverManager = gateway.getDriverManager();
        UbisoldiersDriver driver = new UbisoldiersDriver();
        try {
            driverManager.deployDriver(driver.getDriver(), driver, "");
        } catch(Exception e) {
            throw new RuntimeException("Exception when trying to load driver: " + e.getMessage());
        }

        List<DriverData> driverDataList = gateway.listDrivers("com.github.gobbisanches.ubisoldiers");
        DriverData driverData = driverDataList.get(0);
        UpDriver upDriver = driverData.getDriver();
        String driverName = upDriver.getName();

        if (driverName == "com.github.gobbisanches.ubisoldiers") {
            Log.i("UBISOLDIERS", "Ubisoldiers uOS driver successfully loaded");
        } else {
            throw new RuntimeException("Unable to initialize the Ubisoldiers uOS driver");
        }
    }

    void stopUos() {

    }

    Location getLocation() {
        TreeMap<String, Object> params = new TreeMap<String, Object>();
        ServiceResponse response;

        params.put("CONTEXT", context);

        try {
            response = uos.getGateway().callService(
                    null,
                    "getLocation",
                    "com.github.gobbisanches.ubisoldiers",
                    "",
                    null,
                    params);
        } catch(Exception e) {
            throw new RuntimeException("Could not invoke a call through UOS: " + e.getMessage());
        }

        return (Location) response.getResponseData("LOCATION");
    }

    long performBattle(int playerGeneralId, int opponentGeneralId) {
        TreeMap<String, Object> params = new TreeMap<String, Object>();
        ServiceResponse response;

        params.put("CONTEXT", context);
        params.put("PLAYER_GENERAL_ID", playerGeneralId);
        params.put("OPPONENT_GENERAL_ID", opponentGeneralId);

        try {
            response = uos.getGateway().callService(
                    null,
                    "performBattle",
                    "com.github.gobbisanches.ubisoldiers",
                    "",
                    null,
                    params);
        } catch(Exception e) {
            throw new RuntimeException("Could not invoke a call through UOS: " + e.getMessage());
        }



        return ((Long) response.getResponseData("RANDOM_SEED")).longValue();
    }

    SearchParameters performSearch(int playerGeneralId, Location location, long wifiStrength) {
        TreeMap<String, Object> params = new TreeMap<String, Object>();
        ServiceResponse response;

        params.put("CONTEXT", context);
        params.put("PLAYER_GENERAL_ID", new Integer(playerGeneralId));
        params.put("LOCATION", location);
        params.put("SIGNAL_STRENGTH", new Long(wifiStrength));

        try {
            response = uos.getGateway().callService(
                    null,
                    "performSearch",
                    "com.github.gobbisanches.ubisoldiers",
                    "",
                    null,
                    params);
        } catch(Exception e) {
            throw new RuntimeException("Could not invoke a call through UOS: " + e.getMessage());
        }

        long randomSeed = ((Long) response.getResponseData("RANDOM_SEED")).longValue();
        double modifier = ((Double) response.getResponseData("MODIFIER")).doubleValue();

        return new SearchParameters(randomSeed, modifier);
    }

    long getWifiSignalStrength() {
        TreeMap<String, Object> params = new TreeMap<String, Object>();
        ServiceResponse response;

        params.put("CONTEXT", context);

        try {
            response = uos.getGateway().callService(
                    null,
                    "getWifiSignalStrength",
                    "com.github.gobbisanches.ubisoldiers",
                    "",
                    null,
                    params);
        } catch(Exception e) {
            throw new RuntimeException("Could not invoke a call through UOS: " + e.getMessage());
        }

        long strength = ((Long) response.getResponseData("SIGNAL_STRENGTH")).longValue();

        Log.d("UBISOLDIERS", "[WIFI] level on Manager = " + strength);

        return strength;
    }

    public static UosManager getInstance() {
        if (instance == null) {
            throw new RuntimeException("UosManager was not initialized");
        }

        return instance;
    }

    public static void initInstance(Context context) {
        instance = new UosManager(context);
        instance.startUos();
    }
}




Java Source Code List

com.github.gobbisanches.ubisoldiers.app.ArmyFragment.java
com.github.gobbisanches.ubisoldiers.app.BattleActivity.java
com.github.gobbisanches.ubisoldiers.app.BattleFragment.java
com.github.gobbisanches.ubisoldiers.app.ItemListFragment.java
com.github.gobbisanches.ubisoldiers.app.MainActivity.java
com.github.gobbisanches.ubisoldiers.app.ResourceManager.java
com.github.gobbisanches.ubisoldiers.app.SingleFragmentActivity.java
com.github.gobbisanches.ubisoldiers.app.UbisoldierUosApplication.java
com.github.gobbisanches.ubisoldiers.app.UbisoldiersDriver.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationActivity.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationFragment.java
com.github.gobbisanches.ubisoldiers.app.UnitFragment.java
com.github.gobbisanches.ubisoldiers.app.UosManager.java
com.github.gobbisanches.ubisoldiers.mechanics.Armor.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogEntry.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogParser.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLog.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleSquad.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleUnit.java
com.github.gobbisanches.ubisoldiers.mechanics.Battle.java
com.github.gobbisanches.ubisoldiers.mechanics.DefaultGameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.GameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.General.java
com.github.gobbisanches.ubisoldiers.mechanics.ItemCollection.java
com.github.gobbisanches.ubisoldiers.mechanics.Item.java
com.github.gobbisanches.ubisoldiers.mechanics.MechanicsEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.PolicyManager.java
com.github.gobbisanches.ubisoldiers.mechanics.SearchEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.Soldier.java
com.github.gobbisanches.ubisoldiers.mechanics.Squad.java
com.github.gobbisanches.ubisoldiers.mechanics.Unit.java
com.github.gobbisanches.ubisoldiers.mechanics.Weapon.java