From 3137acdd5a45285dab9903f9d41560c63eca8523 Mon Sep 17 00:00:00 2001 From: "Angelo Mantellini (manangel)" Date: Wed, 29 Mar 2017 18:00:06 +0200 Subject: first commit Change-Id: I8412b8e7d966c2fbc508b537fd9a9bbcfc628ca8 Signed-off-by: Angelo Mantellini (manangel) --- .../metiscontrol/ForwarderStatusActivity.java | 169 ++++++++ .../ccnxsdk/metiscontrol/IMetisAddNewItem.java | 22 ++ .../ccnxsdk/metiscontrol/IMetisNamedFragment.java | 20 + .../ccnx/ccnxsdk/metiscontrol/MetisConstants.java | 76 ++++ .../ccnx/ccnxsdk/metiscontrol/MetisService.java | 128 +++++++ .../ccnxsdk/metiscontrol/MetisStatusFragment.java | 426 +++++++++++++++++++++ 6 files changed, 841 insertions(+) create mode 100644 ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/ForwarderStatusActivity.java create mode 100644 ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisAddNewItem.java create mode 100644 ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisNamedFragment.java create mode 100644 ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisConstants.java create mode 100644 ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisService.java create mode 100644 ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisStatusFragment.java (limited to 'ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol') diff --git a/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/ForwarderStatusActivity.java b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/ForwarderStatusActivity.java new file mode 100644 index 00000000..7bc6bdb3 --- /dev/null +++ b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/ForwarderStatusActivity.java @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.metis.ccnx.ccnxsdk.metiscontrol; + +import android.os.Bundle; +import android.support.design.widget.FloatingActionButton; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class ForwarderStatusActivity extends AppCompatActivity implements + MetisStatusFragment.OnFragmentVisibleListener { + + private static final String TAG = "CCNX FSA"; + + private SectionsPagerAdapter mSectionsPagerAdapter; + + private ViewPager mViewPager; + private Fragment mVisibleFragment; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_forwarder_status); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + mViewPager = (ViewPager) findViewById(R.id.container); + mViewPager.setAdapter(mSectionsPagerAdapter); + + + final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + + assert fab != null; + fab.setVisibility(View.GONE); + + fab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (mVisibleFragment != null) { + if (mVisibleFragment instanceof IMetisAddNewItem) { + IMetisAddNewItem fragment = (IMetisAddNewItem) mVisibleFragment; + fragment.showAddNewItemDialog(); + } + } + } + }); + + } + + + @Override + + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_forwarder_status, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + + int id = item.getItemId(); + + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } + + public static class PlaceholderFragment extends Fragment { + + private static final String ARG_SECTION_NUMBER = "section_number"; + + public PlaceholderFragment() { + } + + public static PlaceholderFragment newInstance(int sectionNumber) { + PlaceholderFragment fragment = new PlaceholderFragment(); + Bundle args = new Bundle(); + args.putInt(ARG_SECTION_NUMBER, sectionNumber); + fragment.setArguments(args); + return fragment; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_forwarder_status, container, false); + TextView textView = (TextView) rootView.findViewById(R.id.section_label); + textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); + return rootView; + } + } + + public class SectionsPagerAdapter extends FragmentPagerAdapter { + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + return MetisStatusFragment.newInstance(position + 1); + } + + @Override + public int getCount() { + return 1; + } + + @Override + public CharSequence getPageTitle(int position) { + return "SECTION 1"; + } + + + } + + public void onFragmentVisible(Fragment fragment) { + Log.d(TAG, "***** PAGE: " + fragment + " is now showing."); + mVisibleFragment = fragment; + + final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + + if (fragment instanceof IMetisAddNewItem) { + fab.setVisibility(View.VISIBLE); + } else { + fab.setVisibility(View.INVISIBLE); + } + + String appName = getResources().getString(R.string.app_name); + + if (fragment instanceof IMetisNamedFragment) { + appName += " // " + ((IMetisNamedFragment) fragment).getFragmentName(); + } + + setTitle(appName); + + } + + + +} diff --git a/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisAddNewItem.java b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisAddNewItem.java new file mode 100644 index 00000000..549f4a68 --- /dev/null +++ b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisAddNewItem.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.metis.ccnx.ccnxsdk.metiscontrol; + +public interface IMetisAddNewItem { + + void showAddNewItemDialog(); + +} diff --git a/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisNamedFragment.java b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisNamedFragment.java new file mode 100644 index 00000000..bc4cddca --- /dev/null +++ b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/IMetisNamedFragment.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.metis.ccnx.ccnxsdk.metiscontrol; + +public interface IMetisNamedFragment { + public String getFragmentName(); +} diff --git a/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisConstants.java b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisConstants.java new file mode 100644 index 00000000..7b9046ae --- /dev/null +++ b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisConstants.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.metis.ccnx.ccnxsdk.metiscontrol; + +public class MetisConstants { + + public static final int MetisDefaultListenerPort = 9695; + + public static final String MetisModule_Control = "Control"; + public static final String MetisModule_FIB = "FIB"; + public static final String MetisModule_PIT = "PIT"; + public static final String MetisModule_ContentStore = "ContentStore"; + public static final String MetisModule_TransportLinkAdapter = "TransportLinkAdapter"; + + public static final String CCNxNameMetis_Forwarder = "ccnx:/local/forwarder"; + public static final String CCNxNameMetis_Control = CCNxNameMetis_Forwarder + "/" + MetisModule_Control; + public static final String CCNxNameMetis_FIB = CCNxNameMetis_Forwarder + "/" + MetisModule_FIB; + public static final String CCNxNameMetis_PIT = CCNxNameMetis_Forwarder + "/" + MetisModule_PIT; + public static final String CCNxNameMetis_ContentStore = CCNxNameMetis_Forwarder + "/" + MetisModule_ContentStore; + ; + public static final String CCNxNameMetis_Link = CCNxNameMetis_Forwarder + "/" + MetisModule_TransportLinkAdapter; + + // General Commands + public static final int MetisCommandSegment = 3; + public static final String MetisCommand_Lookup = "lookup"; + public static final String MetisCommand_Add = "add"; + public static final String MetisCommand_List = "list"; + public static final String MetisCommand_Remove = "remove"; + public static final String MetisCommand_Resize = "resize"; + public static final String MetisCommand_Set = "set"; + public static final String MetisCommand_Quit = "quit"; + public static final String MetisCommand_Run = "spawn"; + public static final String MetisCommand_Stats = "stats"; + + public static final String MetisCommand_LogLevel = "level"; + public static final String MetisCommand_LogDebug = "debug"; + public static final String MetisCommand_LogInfo = "info"; + public static final String MetisCommand_LogError = "error"; + public static final String MetisCommand_LogAll = "all"; + public static final String MetisCommand_LogOff = "off"; + public static final String MetisCommand_LogNotice = "notice"; + ; + + // Module Specific Commands + public static final String CCNxNameMetisCommand_LinkConnect = CCNxNameMetis_Link + "/" + MetisCommand_Add; // create a connection to interface specified in payload, returns name + public static final String CCNxNameMetisCommand_LinkDisconnect = CCNxNameMetis_Link + "/" + MetisCommand_Remove; // remove a connection to interface specified in payload, by name + public static final String CCNxNameMetisCommand_LinkList = CCNxNameMetis_Link + "/" + MetisCommand_List; // list interfaces + + public static final String CCNxNameMetisCommand_FIBLookup = CCNxNameMetis_FIB + "/" + MetisCommand_Lookup; // return current FIB contents for name in payload + public static final String CCNxNameMetisCommand_FIBList = CCNxNameMetis_FIB + "/" + MetisCommand_List; // list current FIB contents + public static final String CCNxNameMetisCommand_FIBAddRoute = CCNxNameMetis_FIB + "/" + MetisCommand_Add; // add route for arguments in payload + public static final String CCNxNameMetisCommand_FIBRemoveRoute = CCNxNameMetis_FIB + "/" + MetisCommand_Remove; // remove route for arguments in payload + + public static final String CCNxNameMetisCommand_PITLookup = CCNxNameMetis_PIT + "/" + MetisCommand_Lookup; // return current PIT contents for name in payload + public static final String CCNxNameMetisCommand_PITList = CCNxNameMetis_PIT + "/" + MetisCommand_List; // list current PIT contents + + public static final String CCNxNameMetisCommand_ContentStoreResize = CCNxNameMetis_ContentStore + "/" + MetisCommand_Resize; // resize current content store to size in MB in payload + + public static final String CCNxNameMetisCommand_Quit = CCNxNameMetis_Control + "/" + MetisCommand_Quit; // ask the forwarder to exit + public static final String CCNxNameMetisCommand_Run = CCNxNameMetis_Control + "/" + MetisCommand_Run; // start a new forwarder instance + public static final String CCNxNameMetisCommand_Set = CCNxNameMetis_Control + "/" + MetisCommand_Set; // set a forwarder variable + public static final String CCNxNameMetisCommand_Stats = CCNxNameMetis_Control + "/" + MetisCommand_Stats; // get forwarder stats +}; diff --git a/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisService.java b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisService.java new file mode 100644 index 00000000..ee5ef5be --- /dev/null +++ b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisService.java @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.metis.ccnx.ccnxsdk.metiscontrol; + +import android.app.Notification; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.util.Log; + +import com.metis.ccnx.ccnxsupportlibrary.Metis; +import android.widget.TextView; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; + +public class MetisService extends Service { + private final static String TAG = "CCNx.MetisService"; + + private static Thread sForwarderThread = null; + + public MetisService() { + } + + private String path; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + + Metis metis = Metis.getInstance(); + if (!metis.isRunning()) { + Log.d(TAG, "Starting Metis"); + String path = null; + if (intent != null && intent.getExtras() != null && intent.getExtras().get("path") != null) { + + path = intent.getExtras().get("path").toString(); + startForwarder(intent, path); + } else { + //TextView mPathTextView = (TextView) view.findViewById(R.id.pathText); + startForwarder(intent, "/storage/emulated/0/MetisConf/metis.cfg".toString()); + } + } else { + Log.d(TAG, "Metis already running."); + } + // Tell Android we want it restarted if it dies or is killed by the OS. + return Service.START_STICKY; + } + + + @Override + public void onDestroy() { + //get Metis instance + Metis metis = Metis.getInstance(); + Log.d(TAG, "Destroying"); + if (metis.isRunning()) { + Log.d(TAG, "Trying to stop Metis: " + metis.toString()); + metis.stop(); + stopForeground(true); + } + super.onDestroy(); + } + + protected Runnable mForwarderRunner = new Runnable() { + + //private String path; + @Override + public void run() { + Metis metis = Metis.getInstance(); + + metis.start(path); + } + + + }; + + + private void startForwarder(Intent intent, String path) { + + int NOTIFICATION_ID = 12345; + + Metis metis = Metis.getInstance(); + if (!metis.isRunning()) { + this.path = path; + sForwarderThread = new Thread(mForwarderRunner, "CCNx.MetisRunner"); + sForwarderThread.start(); + metis.isRunning(); + Intent resultIntent = new Intent(this, ForwarderStatusActivity.class); + + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); + + Notification notification = new Notification.Builder(getApplicationContext()) + .setContentTitle(getString(R.string.app_name)) + .setContentText("Metis is running") + // .setSmallIcon(R.drawable.ic_notification) + .setSmallIcon(R.mipmap.ic_notification) + .setWhen(System.currentTimeMillis()) + .setContentIntent(pendingIntent) + .build(); + + notification.flags |= Notification.FLAG_NO_CLEAR; + + startForeground(NOTIFICATION_ID, notification); + } + } + + +} diff --git a/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisStatusFragment.java b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisStatusFragment.java new file mode 100644 index 00000000..370a53b4 --- /dev/null +++ b/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisStatusFragment.java @@ -0,0 +1,426 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.metis.ccnx.ccnxsdk.metiscontrol; + +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.Handler; +import android.support.design.widget.Snackbar; +import android.support.v4.app.Fragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.CompoundButton; +import android.widget.Spinner; +import android.widget.Switch; +import android.widget.TextView; + +import com.metis.ccnx.ccnxsupportlibrary.Metis; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.net.Inet4Address; +import java.net.InetAddress; +import java.nio.charset.Charset; +import java.util.List; + + +public class MetisStatusFragment extends Fragment implements IMetisNamedFragment { + + private static final String ARG_PAGER_INDEX = "metisstatus_pager_number"; + private static final String TAG = "CCNXMetis SF"; + + // TODO: Rename and change types of parameters + private int mPagerIndex; + + private Switch mSwitchMetisOnOff = null; + private Spinner mSpinnerLogLevel = null; + private Switch mSwitchContentStoreOnOff = null; + private TextView mTVNumInterests = null; + private TextView mTVNumContentObjects = null; + private TextView mTVNumInterestReturns = null; + private TextView mTVNumControlMessages = null; + private TextView mTVNumPITEntries = null; + private TextView mPathTextView = null; + + // Stats counters, updated by background task. + private long mNumInterests = 0; + private long mNumCOs = 0; + private long mNumInterestReturns = 0; + private long mNumControl = 0; + private long mNumPITENtries = 0; + + private boolean mIsStatsQueryRunning = false; + + + //private PortalFactory mPortalFactory = null; + + private OnFragmentVisibleListener mListener; + + + /** + * Create a Handler and a Runnable to be called every few seconds to query + * Metis (when running) for stats. + */ + private Handler mStatusUpdaterHandler = new Handler(); + private Runnable mStatusUpdateRunnable = new Runnable() { + @Override + public void run() { + // This runs on the main thread, so start an AsyncTask + // Repeat this the same runnable code block again another few seconds + //new GetStatusTask(null).execute(mPortalFactory); + //if (mIsStatsQueryRunning) { + //mStatusUpdaterHandler.postDelayed(mStatusUpdateRunnable, 2 * 1000); + //} + } + }; + + + public MetisStatusFragment() { + // Required empty public constructor + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @return A new instance of fragment MetisStatusFragment. + */ + // TODO: Rename and change types and number of parameters + public static MetisStatusFragment newInstance(int pagerIndex) { + MetisStatusFragment fragment = new MetisStatusFragment(); + Bundle args = new Bundle(); + args.putInt(ARG_PAGER_INDEX, pagerIndex); + + fragment.setArguments(args); + + return fragment; + } + + @Override + public void onStart() { + Metis metis = Metis.getInstance(); + mSwitchMetisOnOff.setChecked(metis.isRunning()); + super.onStart(); + } + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mPagerIndex = getArguments().getInt(ARG_PAGER_INDEX); + } + + + Log.d(TAG, "Creating new PortalFactory"); + //Identity identity = CCNxUtils.createCCNxIdentity(getContext(), + // "password", "ccnxsdkdemo", 1024, 30); + //mPortalFactory = new PortalFactory(identity); + } + + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_metis_status, container, false); + + mSwitchMetisOnOff = (Switch) view.findViewById(R.id.switchMetisOnOff); + mSwitchContentStoreOnOff = (Switch) view.findViewById(R.id.switchMetisContentStoreOnOff); + mSpinnerLogLevel = (Spinner) view.findViewById(R.id.spinnerMetisLoggingLevel); + mPathTextView = (TextView) view.findViewById(R.id.pathText) ; + + mSpinnerLogLevel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + String loggingLevel = mSpinnerLogLevel.getSelectedItem().toString(); + updateMetisLoggingLevel(loggingLevel); + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + + mSwitchMetisOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + startMetis(); + //mSpinnerLogLevel.setEnabled(true); + //Log.d(TAG, "################# Start periodic query for stats"); + //if (!mIsStatsQueryRunning) { + // mStatusUpdaterHandler.postDelayed(mStatusUpdateRunnable, 500); + // mIsStatsQueryRunning = true; + // String loggingLevel = mSpinnerLogLevel.getSelectedItem().toString(); + // if (!loggingLevel.equalsIgnoreCase("off")) { + // updateMetisLoggingLevel(loggingLevel); + // } + //} + } else { + Log.d(TAG, "################# Stop periodic query for stats"); + //mStatusUpdaterHandler.removeCallbacks(mStatusUpdateRunnable); + //mIsStatsQueryRunning = false; + stopMetis(); + //mSpinnerLogLevel.setEnabled(false); + //mSpinnerLogLevel.setSelection(0); + } + } + }); + + mTVNumInterests = (TextView) view.findViewById(R.id.tvStatsNumInterests); + mTVNumContentObjects = (TextView) view.findViewById(R.id.tvStatsNumContentObjects); + mTVNumInterestReturns = (TextView) view.findViewById(R.id.tvStatsNumInterestReturns); + mTVNumControlMessages = (TextView) view.findViewById(R.id.tvStatsNumControl); + mTVNumPITEntries = (TextView) view.findViewById(R.id.tvStatsPITSize); + + mTVNumInterests.setText(String.valueOf(mNumInterests)); + mTVNumContentObjects.setText(String.valueOf(mNumCOs)); + mTVNumControlMessages.setText(String.valueOf(mNumControl)); + mTVNumInterestReturns.setText(String.valueOf(mNumInterestReturns)); + mTVNumPITEntries.setText(""); + + return view; + } + + private void startMetis() { + mPathTextView.setEnabled(false); + Metis metis = Metis.getInstance(); + if (!metis.isRunning()) { + Intent intent = new Intent(getActivity(), MetisService.class); + intent.putExtra("path", mPathTextView.getText()); + getActivity().startService(intent); + + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + createExternalListeners(); + } + }, 1000); + } + } + + private void stopMetis() { + mPathTextView.setEnabled(true); + Intent intent = new Intent(getActivity(), MetisService.class); + + getActivity().stopService(intent); + + } + + private void updateMetisLoggingLevel(String loggingLevel) { + /*Metis metis = Metis.getInstance(); + if (metis.isRunning()) { + // Send an Interest control message to Metis with the new logging level. + String commandURI = MetisConstants.CCNxNameMetisCommand_Set + "/" + MetisConstants.MetisCommand_LogLevel + "/" + loggingLevel; + Name name = new Name(commandURI); + SendInterestTask task = new SendInterestTask(name, null, new SendInterestTask.OnInterestSentListener() { + @Override + public void onInterestSent(Message message) { + if (message instanceof ContentObject) { + + String responseString = new String(((ContentObject) message).payload()); + Snackbar snackbar = Snackbar + .make(mSwitchMetisOnOff, responseString, Snackbar.LENGTH_SHORT); + + snackbar.show(); + } else { + Log.d(TAG, "Unexpected non-Content response from sent Interest"); + } + } + }); + + task.execute(mPortalFactory); + }*/ + } + + private void createExternalListeners() { + + /*Metis metis = Metis.getInstance(); + + if (metis.isRunning()) { + + List ipAddresses = CCNxUtils.getLocalIpAddress(); + + for (InetAddress addr : ipAddresses) { + + // For the moment, just listen on the IPV4 addresses. The V6 addresses should work, + // but it's not yet tested. + + if (addr instanceof Inet4Address) { + + String ipAddress = addr.getHostAddress(); + + Log.d(TAG, "Adding external listener on: " + ipAddress); + + String linkURI = "tcp://" + ipAddress + ":" + MetisConstants.MetisDefaultListenerPort + "/listener"; + + Name name = new Name(MetisConstants.CCNxNameMetisCommand_LinkConnect); + + SendInterestTask task = new SendInterestTask(name, linkURI.getBytes(), new SendInterestTask.OnInterestSentListener() { + @Override + public void onInterestSent(Message message) { + if (message instanceof ContentObject) { + + String responseString = new String(((ContentObject) message).payload()); + Snackbar snackbar = Snackbar + .make(mSwitchMetisOnOff, responseString, Snackbar.LENGTH_SHORT); + + snackbar.show(); + } else { + Log.d(TAG, "Unexpected non-Content response from sent Interest"); + } + } + }); + + task.execute(mPortalFactory); + } + } + }*/ + } + + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof OnFragmentVisibleListener) { + mListener = (OnFragmentVisibleListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + + @Override + public void setUserVisibleHint(boolean isVisibleToUser) { + super.setUserVisibleHint(isVisibleToUser); + Metis metis = Metis.getInstance(); + + if (isVisibleToUser) { + mListener.onFragmentVisible(this); + + if (metis.isRunning()) { + // Begin updating stats. + if (!mIsStatsQueryRunning) { + mStatusUpdaterHandler.postDelayed(mStatusUpdateRunnable, 100); + mIsStatsQueryRunning = true; + } + } + } else { + mStatusUpdaterHandler.removeCallbacks(mStatusUpdateRunnable); + mIsStatsQueryRunning = false; + } + } + + @Override + public void onDetach() { + mStatusUpdaterHandler.removeCallbacks(mStatusUpdateRunnable); + mIsStatsQueryRunning = false; + super.onDetach(); + mListener = null; + } + + @Override + public String getFragmentName() { + return "Status"; + } + + public interface OnFragmentVisibleListener { + // TODO: Update argument type and name + void onFragmentVisible(Fragment which); + } + + /*private class GetStatusTask extends AsyncTask { + + private boolean mSuccess = false; + private PortalFactory mPortalFactory = null; + + + public GetStatusTask(String unused) { + } + + @Override + protected Integer doInBackground(PortalFactory... args) { + Thread.currentThread().setName("GetStatusTask-Async"); + + mPortalFactory = args[0]; + try { + Name controlName = new Name(MetisConstants.CCNxNameMetisCommand_Stats); + + Interest interest = new Interest(controlName); + + try { + Portal portal = mPortalFactory.getPortal(); + + portal.send(interest, 0L); + + Message m = portal.receive(0L); + + if (m instanceof ContentObject) { + mSuccess = true; + ContentObject co = (ContentObject) m; + byte[] payload = co.payload(); + + if (payload != null) { + String jsonString = new String(payload, Charset.defaultCharset()); + //Log.d(TAG, "Received: XX " + jsonString + " XX"); + try { + JSONObject jo = new JSONObject(jsonString); + //Log.d(TAG, "JSON2: " + jo.toString(2)); + + mNumInterests = jo.getLong("numProcessedInterests"); + mNumCOs = jo.getLong("numProcessedContentObjects"); + mNumControl = jo.getLong("numProcessedControlMessages"); + mNumInterestReturns = jo.getLong("numProcessedInterestReturns"); + } catch (JSONException ex) { + Log.e(TAG, "Could not parse returned JSON: " + ex.getMessage()); + } + } + } + portal.close(); + } catch (Portal.CommunicationsError ex) { + Log.e(TAG, "Error sending AddLink command: " + ex.getMessage()); + } + } catch (Exception ex) { + Log.e(TAG, "Error adding link: " + ex.getMessage()); + } + + return 1; + } + + @Override + protected void onPostExecute(Integer ignored) { + + if (mSuccess) { + mTVNumInterests.setText(String.valueOf(mNumInterests)); + mTVNumContentObjects.setText(String.valueOf(mNumCOs)); + mTVNumControlMessages.setText(String.valueOf(mNumControl)); + mTVNumInterestReturns.setText(String.valueOf(mNumInterestReturns)); + mTVNumPITEntries.setText(""); + } + } + }*/ + +} -- cgit 1.2.3-korg