Forumsee logo
Forumsee » Mobility » Windows phone » Read message
RSS: Subscribe to RSS

UDP message handling - spaghetti code. Is there a better way?

Windows Phone Source - winsource.com
Inheritance hierarchy

AppCompatibleActivity -> CBaseActivity -> CWifiActivity -> CUDPActivity -> CSearchAutoSolActivity

AsyncTask ->CSearchTask

Thread -> CUDPThread

Handler -> CUDPHandler

Basically I am trying to create re-usable code with this hierarchy so that I don't have to 're-ivent the wheel' every time I want to write an app that sends and receives UDP messages!

If this sample app () is accurate then basically I am forced into this arrangement:

1) CSearchAutoSolActivity object - click search button

2) Create CSearchTask object - iterate through remote irrigation stations 1-12 and update UI controls as search proceeds

3) Create CUDPThread object and load it with the message to be sent over UDP, the destination IP address and the port number.

4) Create a CUDPHandler object as a message buffer between the CUDPThreadand the CSearchAutoSolActivity.

.

.

.

1) CSearchTask object creates the message to be sent over UDP and gives it to CUDPThread

2) CUDPThread sends the UDP message and waits for a response.

3) CUDPThread sends a message to CUDPHandler detailing if a response was received or not.

4) CUDPHandler pass the message on to CSearchAutoSolActivity through some virtual functions I setup in it and its base class.

5) The virtual functions in CSearchAutoSolActivity passes the UDP response (or no response) on to CSearchTask so that it can update the UI controls in CSearchAutoSolActivity

In Microsoft Visual Studio I can accomplish all this in a single custom function in my CDialog derived class!

Unless there is a simpler way to do all this that escapes me, this all seems like spaghetti code to me and I certainly don't want to have to re-create it afresh with every new app I write that uses UDP messaging.

Is there a simpler way to do all this in Android Studio????

And I am still no closer to successfully sending a UDP message under Android studio because my app is reporting that the local IP address is 127.0.0.1.

And I am told this means I am not connected to the network!

WHY is it so difficult and complicated to send a UDP message with Android Studio?

It took me about a day to figure it out and get it working with Microsoft Visual Studio!

But Android Studio is proving to be a damn nightmare!

class CSearchAutoSolActivity and class CSearchTask

Code (Text):

-  

- package com.example.greg.irrigationcontroller;

-  

- import android.content.Intent;

- import android.os.AsyncTask;

- import android.os.Bundle;

- import android.util.Log;

- import android.view.View;

- import android.widget.Button;

- import android.widget.ProgressBar;

- import android.widget.TextView;

- import java.io.IOException;

- import java.net.DatagramPacket;

- import java.net.DatagramSocket;

- import java.net.InetAddress;

-  

- /**

- * Created by Greg on 5/10/2017.

- */

-  

- public class CSearchAutoSolActivity extends CUDPActivity implements View.OnClickListener

- {

-     @Override

-     protected void onCreate(Bundle savedInstanceState)

-     {

-         super.onCreate(savedInstanceState);

-         setContentView(R.layout.activity_search_auto_sol);

-  

-         if (m_app != null)

-             m_app.SetCurrentActivity(CSearchAutoSolActivity.this);

-         m_textviewResults = (TextView)findViewById(R.id.id_textview_results);

-         m_textviewTrying = (TextView)findViewById(R.id.id_textview_trying);

-         m_progress = (ProgressBar)findViewById(R.id.id_progressbar_search);

-         m_buttonStart = (Button)findViewById(R.id.id_button_start);

-         m_buttonStart.setOnClickListener(CSearchAutoSolActivity.this);

-         m_buttonBack = (Button)findViewById(R.id.id_button_back);

-         m_buttonBack.setOnClickListener(CSearchAutoSolActivity.this);

-  

-         m_handlerUDPClient = new CUDPHandler(this);

-     }

-  

-     @Override

-     public void onClick(View view)

-     {

-         Intent intent = null;

-  

-         switch (view.getId())

-         {

-             case R.id.id_button_start:

-                 m_buttonStart.setEnabled(false);

-                 m_searchtask = new CSearchTask(this);

-                 m_searchtask.execute();

-                 break;

-             case R.id.id_button_back:

-                 intent = new Intent(getApplicationContext(), CMainActivity.class);

-                 break;

-         }

-         if (intent != null)

-         {

-             finish();

-             startActivity(intent);

-         }

-     }

-  

-     @Override

-     public void updateState(final String strMsg)

-     {

-         Log.d("CSearchAutoSolActivity", strMsg);

-     }

-  

-     @Override

-     public void updateMessageSent(final String strMsg)

-     {

-         Log.d("CSearchAutoSolActivity", strMsg);

-     }

-  

-     @Override

-     public void updateMessageReceived(final String strMsg)

-     {

-         if (strMsg.contains("notify"))

-         {

-             String strIPAddr = new String(), strField = new String();

-             int nStation = 0;

-  

-             deleteField(strMsg, "notify");

-             strField = getNextField(strMsg);

-             nStation = Integer.parseInt(strField);

-             deleteField(strMsg, strField);

-             strIPAddr = getNextField(strMsg);

-  

-             m_searchtask.updateStationResponse(nStation, strIPAddr);

-             m_app.SetStationActive(nStation, true);

-             m_app.SetStationIPAddress(nStation, strIPAddr);

-         }

-     }

-  

-     @Override

-     public void updateNoResponse(final String strMsg)

-     {

-         if (strMsg.contains("request"))

-         {

-             String strField = new String();

-             int nStation = 0;

-  

-             deleteField(strMsg, "request");

-             strField = getNextField(strMsg);

-             nStation = Integer.parseInt(strField);

-  

-             m_searchtask.updateStationNoResponse(nStation);

-             m_app.SetStationActive(nStation, false);

-         }

-     }

-  

-     @Override

-     public void updateEnd(final String strMsg)

-     {

-         m_buttonStart.setEnabled(true);

-     }

-  

-     @Override

-     public void updateException(final String strMsg)

-     {

-         m_strDebug = strMsg;

-     }

-  

-     protected TextView m_textviewResults, m_textviewTrying;

-     protected Button m_buttonStart, m_buttonBack;

-     protected ProgressBar m_progress;

-     protected CSearchTask m_searchtask;

-  

-  

-     protected class CSearchTask extends AsyncTask<Void/**param type*/, Void/**progress type*/, String/**result type*/>

-     {

-         public CSearchTask(final CUDPActivity actParent)

-         {

-             m_actParent = actParent;

-             m_strResults = new String();

-             m_strTrying = new String();

-             m_nProgress = 0;

-         }

-  

-         public void updateStationResponse(final int nStation, final String strIPAddr)

-         {

-             m_strResults += "Station " + String.valueOf(nStation) + ": response received from " + strIPAddr + "!";

-         }

-  

-         public void updateStationNoResponse(final int nStation)

-         {

-             m_strResults += "Station " + String.valueOf(nStation) + ": no response!";

-         }

-  

-         @Override

-         protected String doInBackground(Void...Void)

-         {

-             String strResult = new String("");

-  

-             searchForStations();

-  

-             return strResult;

-         }

-  

-         @Override

-         protected void onProgressUpdate(Void...param)

-         {

-             m_textviewResults.setText(m_strResults);

-             m_textviewTrying.setText(m_strTrying);

-             m_progress.setProgress(m_nProgress);

-         }

-  

-         @Override

-         protected void onPostExecute(String strResult)

-         {

-             m_buttonStart.setEnabled(true);

-             m_textviewTrying.setText("Finished");

-         }

-  

-         @Override

-         protected void onPreExecute()

-         {

-         }

-  

-         protected void searchForStations()

-         {

-             if ((m_app != null) && (m_wifiManag != null) && m_wifiManag.isWifiEnabled() && (m_textviewResults != null) && (m_textviewTrying != null))

-             {

-                 CIPAddress ipAddrLocal = new CIPAddress();

-                 ipAddrLocal.setLocalIPAddr();

-  

-                 String strRequest = new String("request") + m_strUDPDelim, strMsg = new String();

-                 int nProgressInc = 100 / m_app.m_nMaxStations + 1;

-  

-                 for (int nStation = 1; nStation <= m_app.m_nMaxStations; nStation++)

-                 {

-                     m_strTrying = new String("Trying station ") + String.valueOf(nStation) + new String("...");

-                     publishProgress();

-                     strMsg = strRequest + String.valueOf(nStation) + m_strUDPDelim + ipAddrLocal.toString() + m_strUDPDelim;

-                     sendMessage(m_actParent, strMsg, ipAddrLocal.toBroadcast(), m_nUDPPort, true);

-                     m_nProgress += nProgressInc;

-                 }

-                 publishProgress();

-             }

-         }

-  

-         protected String m_strResults, m_strTrying;

-         protected int m_nProgress;

-         protected CUDPActivity m_actParent = null;

-     }

- }

-  

class CUDPActivity

Code (Text):

-  

- package com.example.greg.irrigationcontroller;

-  

- import android.os.Bundle;

-  

-  

-  

- /**

- * Created by Greg on 8/10/2017.

- */

-  

- public class CUDPActivity extends CWifiActivity

- {

-     @Override

-     protected void onCreate(Bundle savedInstanceState)

-     {

-         super.onCreate(savedInstanceState);

-     }

- .

- .

- .

- .

- .

- .

- }

-  

class CUDPActivity

Code (Text):

-  

- package com.example.greg.irrigationcontroller;

-  

- import android.os.Bundle;

-  

-  

-  

- /**

- * Created by Greg on 8/10/2017.

- */

-  

- public class CUDPActivity extends CWifiActivity

- {

-     @Override

-     protected void onCreate(Bundle savedInstanceState)

-     {

-         super.onCreate(savedInstanceState);

-     }

-  

-     String getNextField(final String strMsg)

-     {

-         int nPos = strMsg.indexOf(m_strUDPDelim);

-         String strField = strMsg.substring(0, nPos);

-         return strField;

-     }

-  

-     String deleteField(String strMsg, final String strField)

-     {

-         int nPos = strMsg.indexOf(m_strUDPDelim);

-         strMsg = strMsg.substring(nPos);

-         return strMsg;

-     }

-  

-     public void updateState(final String strMsg)

-     {

-     }

-  

-     public void updateMessageSent(final String strMsg)

-     {

-     }

-  

-     public void updateMessageReceived(final String strMsg)

-     {

-     }

-  

-     public void updateNoResponse(final String strMsg)

-     {

-     }

-  

-     public void updateEnd(final String strMsg)

-     {

-     }

-  

-     public void updateException(final String strMsg)

-     {

-     }

-  

-     protected void sendMessage(final CUDPActivity actUDP, final String strMessage, final String strIPAddr, final int nPort, final boolean bBroadcast)

-     {

-         if ((m_app != null) && (m_wifiManag != null) && m_wifiManag.isWifiEnabled())

-         {

-             //m_handlerUDPClient = new CUDPHandler(actUDP);

-             //CUDPThread threadUDPClient = new CUDPThread(strIPAddr, nPort, strMessage, handlerUDPClient);

-             //threadUDPClient.start();

-         }

-     }

-     protected final String m_strUDPDelim = "`";

-     protected final int m_nUDPPort = 10002;

-     protected CUDPHandler m_handlerUDPClient = null;

-     protected CUDPThread m_threadUDPClient = null;

- }

-  

class CUDPHandler

Code (Text):

-  

- package com.example.greg.irrigationcontroller;

-  

- import android.os.Handler;

- import android.os.Message;

-  

- class CUDPHandler extends Handler

- {

-    public static final int UPDATE_STATE = 0;

-     public static final int UPDATE_MSG_SENT = 1;

-     public static final int UPDATE_MSG_REC = 2;

-     public static final int UPDATE_NO_RESP = 3;

-     public static final int UPDATE_END = 4;

-     public static final int UPDATE_EXCEPTION = 5;

-  

-    private CUDPActivity m_actParent = null;

-  

-    public CUDPHandler(CUDPActivity actParent)

-    {

-       super();

-         m_actParent = actParent;

-    }

-  

-    @Override

-    public void handleMessage(Message msg)

-    {

-       switch (msg.what)

-       {

-          case UPDATE_STATE:

-             m_actParent.updateState((String)msg.obj);

-             break;

-             case UPDATE_MSG_SENT:

-                 m_actParent.updateMessageSent((String)msg.obj);

-                 break;

-             case UPDATE_MSG_REC:

-                 m_actParent.updateMessageReceived((String)msg.obj);

-                 break;

-             case UPDATE_NO_RESP:

-                 m_actParent.updateNoResponse((String)msg.obj);

-                 break;

-             case UPDATE_END:

-                 m_actParent.updateEnd((String)msg.obj);

-                 break;

-             case UPDATE_EXCEPTION:

-                 m_actParent.updateException((String)msg.obj);

-                 break;

-          default:

-             super.handleMessage(msg);

-       }

-    }

- }

-  

 
Date: Oct 12, 2017   



Tags

Cars ·
Travel ·
production-frontend