Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lite Refactor of PhoneHomeThread #95

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
195 changes: 106 additions & 89 deletions Source/mesquite/trunk/PhoneHomeThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,129 +13,146 @@
*/
package mesquite.trunk;


import java.io.IOException;
import java.util.Vector;

import org.apache.commons.httpclient.NameValuePair;

import mesquite.lib.*;
import mesquite.lib.AlertDialog;
import mesquite.lib.ListableVector;
import mesquite.lib.MesquiteFile;
import mesquite.lib.MesquiteInteger;
import mesquite.lib.MesquiteMessage;
import mesquite.lib.MesquiteModule;
import mesquite.lib.MesquiteModuleInfo;
import mesquite.lib.MesquiteThread;
import mesquite.lib.MesquiteTrunk;
import mesquite.lib.PhoneHomeRecord;
import mesquite.lib.PhoneHomeUtil;
import mesquite.lib.StringUtil;
import mesquite.tol.lib.BaseHttpRequestMaker;

/* ======================================================================== */
/**
* Phone Home to mesquite server. At thread startup, PhoneHomeThread will report
* the mesquite version to the Mesquite server. It will then proceed to query
* the mesquite server for any information about the installed module.
*
* After the initial startup, Phone Home thread will attempt to post any "beans"
* to the mesquite server every ten seconds
*/
public class PhoneHomeThread extends Thread {
Vector beans = new Vector();
public PhoneHomeThread () {
private Vector<NameValuePair[]> beans = new Vector<NameValuePair[]>();

public PhoneHomeThread() {
setPriority(Thread.MIN_PRIORITY);
}

@Override
public void run() {
/*NOTICES =====Checking website to see if there are any notices or updates*/
//Put here on a separate thread so Mesquite doesn't hang if website is unavailable
/*
* TODO Make this call non-blocking so the phone home thread does not hang if
* website is unavailable
*/
checkForMessagesFromAllHomes();

while (!MesquiteTrunk.mesquiteExiting) {
// Report beans to the Mesqutie server
while (!MesquiteTrunk.mesquiteExiting) {
try {
Thread.sleep(1000);
if (beans.size()>0){
NameValuePair[] b = (NameValuePair[])beans.elementAt(0);
if (beans.size() > 0) {
BaseHttpRequestMaker.sendInfoToServer(beans.elementAt(0), MesquiteModule.beansReportURL, null, 0);
beans.removeElementAt(0);
BaseHttpRequestMaker.sendInfoToServer(b, MesquiteModule.beansReportURL, null, 0);
}
}
catch (Throwable e){
}
} catch (InterruptedException e) { // Kill thread if Interrupted
MesquiteTrunk.mesquiteTrunk.logln("PhoneHomeThread was interrupted");
e.printStackTrace();
break;
}
}
}
public void postBean(NameValuePair[] pairs){

public void postBean(NameValuePair[] pairs) {
beans.addElement(pairs);
}
/*.................................................................................................................*/
public void checkForMessagesFromAllHomes(){
//MesquiteTrunk.incrementMenuResetSuppression();

try {
if (!MesquiteTrunk.suppressVersionReporting){
StringBuffer response = new StringBuffer();
String buildNum = Integer.toString(MesquiteTrunk.getBuildNumber());
if (MesquiteTrunk.mesquiteTrunk.isPrerelease())
buildNum = "PreRelease-" + buildNum;
BaseHttpRequestMaker.contactServer(buildNum, MesquiteModule.versionReportURL, response);
String r = response.toString();
//if mq3rs is included in response, then this is real response
if (!StringUtil.blank(r) && r.indexOf("mq3rs")>=0){
if (r.indexOf("mq3rsshow")>=0){ //show dialog at startup!!!!
AlertDialog.noticeHTML(MesquiteTrunk.mesquiteTrunk.containerOfModule(),"Note", r, 600, 400, null);
}

/**
* Reports version to Mesquite server and checks for information about mesquite
* and installed modules
*/
public void checkForMessagesFromAllHomes() {
// Report Version to server
if (!MesquiteTrunk.suppressVersionReporting) {
StringBuffer response = new StringBuffer();
String buildNum = Integer.toString(MesquiteTrunk.getBuildNumber());
if (MesquiteTrunk.mesquiteTrunk.isPrerelease())
buildNum = "PreRelease-" + buildNum;

BaseHttpRequestMaker.contactServer(buildNum, MesquiteModule.versionReportURL, response);

String r = response.toString();
if (!StringUtil.blank(r) && r.indexOf("mq3rs") >= 0) {
if (r.indexOf("mq3rsshow") >= 0) { // show dialog at startup!!!!
AlertDialog.noticeHTML(MesquiteTrunk.mesquiteTrunk.containerOfModule(), "Note", r, 600, 400, null);
}
else if (MesquiteTrunk.debugMode)
MesquiteMessage.warnProgrammer("no response or incorrect response from server on startup");
}
}
catch (Throwable t){
if (MesquiteTrunk.debugMode)
MesquiteMessage.warnProgrammer("PROBLEM PHONING HOME to report version\n" + t.getCause());
} else if (MesquiteTrunk.debugMode)
MesquiteMessage.warnProgrammer("no response or incorrect response from server on startup");
}

// Check Server for notice regarding the mesquite and the various installed
// modules
ListableVector phoneRecords = new ListableVector();
StringBuffer notices = new StringBuffer();
StringBuffer logBuffer = new StringBuffer();
String path = MesquiteModule.prefsDirectory+ MesquiteFile.fileSeparator+ "phoneRecords.xml";
String path = MesquiteModule.prefsDirectory + MesquiteFile.fileSeparator + "phoneRecords.xml";
PhoneHomeUtil.readOldPhoneRecords(path, phoneRecords);
for (int i = 0; i < MesquiteTrunk.mesquiteModulesInfoVector.size(); i++) {
MesquiteModuleInfo mmi = (MesquiteModuleInfo) MesquiteTrunk.mesquiteModulesInfoVector.elementAt(i);
if (StringUtil.blank(mmi.getHomePhoneNumber())) {
continue;
}
int rec = phoneRecords.indexOfByName("#" + mmi.getClassName());
if (MesquiteTrunk.debugMode) {
MesquiteTrunk.mesquiteTrunk.logln("Checking server for notices regarding " + mmi.getPackageName());
}

for (int i= 0; i<MesquiteTrunk.mesquiteModulesInfoVector.size(); i++){
MesquiteModuleInfo mmi = (MesquiteModuleInfo)MesquiteTrunk.mesquiteModulesInfoVector.elementAt(i);
if (!StringUtil.blank(mmi.getHomePhoneNumber())) {
try {
int rec = phoneRecords.indexOfByName("#" + mmi.getClassName());
if (MesquiteTrunk.debugMode){
MesquiteTrunk.mesquiteTrunk.logln("Checking server for notices regarding " + mmi.getPackageName());
}

PhoneHomeRecord phoneHomeRecord;
if (!MesquiteInteger.isCombinable(rec) || rec<0) {// this module is not the phone records
phoneHomeRecord = new PhoneHomeRecord("#"+mmi.getClassName());
phoneRecords.addElement(phoneHomeRecord, false);
}
else
phoneHomeRecord = (PhoneHomeRecord)phoneRecords.elementAt(rec);
String notice = PhoneHomeUtil.retrieveMessagesFromHome(mmi, phoneHomeRecord, logBuffer);

phoneHomeRecord.setCurrentValues(mmi);
if (!StringUtil.blank(notice)) {
if (mmi.getModuleClass() == mesquite.Mesquite.class)
notices.append("<h3>From Mesquite</h3>");
else if (!StringUtil.blank(mmi.getPackageName()))
notices.append("<h3>From " + mmi.getPackageName() + "</h3>");
else
notices.append("<h3>From " + mmi.getName() + "</h3>");
notices.append(notice);
//notices.append("<hr>");
}
}
catch (Throwable t){
}
PhoneHomeRecord phoneHomeRecord;
if (!MesquiteInteger.isCombinable(rec) || rec < 0) {// this module is not the phone records
phoneHomeRecord = new PhoneHomeRecord("#" + mmi.getClassName());
phoneRecords.addElement(phoneHomeRecord, false);
} else
phoneHomeRecord = (PhoneHomeRecord) phoneRecords.elementAt(rec);

String notice = "";
notice = PhoneHomeUtil.retrieveMessagesFromHome(mmi, phoneHomeRecord, logBuffer);
phoneHomeRecord.setCurrentValues(mmi);
if (!StringUtil.blank(notice)) {
if (mmi.getModuleClass() == mesquite.Mesquite.class)
notices.append("<h3>From Mesquite</h3>");
else if (!StringUtil.blank(mmi.getPackageName()))
notices.append("<h3>From " + mmi.getPackageName() + "</h3>");
else
notices.append("<h3>From " + mmi.getName() + "</h3>");
notices.append(notice);
}
}
if (!StringUtil.blank(logBuffer.toString())){
MesquiteTrunk.mesquiteTrunk.logln("\n*************************" + logBuffer.toString() + "\n*************************\n");
}

if (!StringUtil.blank(notices)){
String note = ("<h2>Notices from the websites of Mesquite and installed packages</h2><hr>" + notices.toString() + "<br><h4>(You can ask Mesquite not to check for messages on its websites using the menu item in the Defaults submenu of the File menu)</h4>");
if (!MesquiteThread.isScripting()){
AlertDialog.noticeHTML(MesquiteTrunk.mesquiteTrunk.containerOfModule(),"Note", note, 600, 400, PhoneHomeUtil.getPhoneHomeDialogLinkCommand(), true);
}
else
// Print Notices to console
if (!StringUtil.blank(logBuffer.toString())) {
MesquiteTrunk.mesquiteTrunk
.logln("\n*************************" + logBuffer.toString() + "\n*************************\n");
}
if (!StringUtil.blank(notices)) {
String note = ("<h2>Notices from the websites of Mesquite and installed packages</h2><hr>"
+ notices.toString()
+ "<br><h4>(You can ask Mesquite not to check for messages on its websites using the menu item in the Defaults submenu of the File menu)</h4>");
if (!MesquiteThread.isScripting()) {
AlertDialog.noticeHTML(MesquiteTrunk.mesquiteTrunk.containerOfModule(), "Note", note, 600, 400,
PhoneHomeUtil.getPhoneHomeDialogLinkCommand(), true);
} else
System.out.println(note);
}
if (phoneRecords.size()>0)
if (phoneRecords.size() > 0)
PhoneHomeUtil.writePhoneRecords(path, phoneRecords);
MesquiteTrunk.mesquiteTrunk.storePreferences();
MesquiteTrunk.resetAllMenuBars();

// MesquiteTrunk.decrementMenuResetSuppression();
}

}