/** ** Main client thread loop */ public void run() { this.setRunStatus(THREAD_RUNNING); this.threadStarted(); try { this.openSocket(); this.inputThread = new InputThread(this.socket, this.readTimeout, this.ioThreadLock); this.outputThread = new OutputThread(this.socket, this.ioThreadLock); this.inputThread.start(); this.outputThread.start(); synchronized (this.ioThreadLock) { while (this.inputThread.isRunning() || this.outputThread.isRunning()) { try { this.ioThreadLock.wait(); } catch (Throwable t) { } } } } catch (NoRouteToHostException nrthe) { Print.logInfo("Client:ControlThread - Unable to reach " + this.host + ":" + this.port); nrthe.printStackTrace(); } catch (Throwable t) { Print.logInfo("Client:ControlThread - " + t); t.printStackTrace(); } finally { this.closeSocket(); } this.setRunStatus(THREAD_STOPPED); this.threadStopped(); }
public void run() { StringBuffer data = new StringBuffer(); Print.logDebug("Client:InputThread started"); while (true) { data.setLength(0); boolean timeout = false; try { if (this.readTimeout > 0L) { this.socket.setSoTimeout((int) this.readTimeout); } ClientSocketThread.socketReadLine(this.socket, -1, data); } catch (InterruptedIOException ee) { // SocketTimeoutException ee) { // error("Read interrupted (timeout) ..."); if (getRunStatus() != THREAD_RUNNING) { break; } timeout = true; // continue; } catch (Throwable t) { Print.logError("Client:InputThread - " + t); t.printStackTrace(); break; } if (!timeout || (data.length() > 0)) { ClientSocketThread.this.handleMessage(data.toString()); } } synchronized (this.threadLock) { this.isRunning = false; Print.logDebug("Client:InputThread stopped"); this.threadLock.notify(); } }
/* start UDP listener */ private ServerSocketThread _startUDP(int port) throws Throwable { ServerSocketThread sst = null; /* create server socket */ try { sst = new ServerSocketThread(ServerSocketThread.createDatagramSocket(port)); } catch (Throwable t) { // trap any server exception Print.logException("ServerSocket error", t); throw t; } /* initialize */ sst.setTextPackets(Constants.ASCII_PACKETS); sst.setBackspaceChar(null); // no backspaces allowed sst.setLineTerminatorChar(Constants.ASCII_LINE_TERMINATOR); sst.setIgnoreChar(Constants.ASCII_IGNORE_CHARS); sst.setMaximumPacketLength(Constants.MAX_PACKET_LENGTH); sst.setMinimumPacketLength(Constants.MIN_PACKET_LENGTH); sst.setIdleTimeout(TrackServer.udpTimeout_idle); sst.setPacketTimeout(TrackServer.udpTimeout_packet); sst.setSessionTimeout(TrackServer.udpTimeout_session); sst.setTerminateOnTimeout(Constants.TERMINATE_ON_TIMEOUT); sst.setClientPacketHandlerClass(TrackClientPacketHandler.class); /* start thread */ Print.logInfo( "Starting UDP listener thread on port " + port + " [timeout=" + sst.getSessionTimeout() + "ms] ..."); sst.start(); this.udpThread.add(sst); return sst; }
protected DCServerFactory.ResultCode sendEmail( String frEmail, String toEmail, String subj, String body) { if (StringTools.isBlank(frEmail)) { Print.logError("'From' Email address not specified"); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } else if (StringTools.isBlank(toEmail) || !CommandPacketHandler.validateAddress(toEmail)) { Print.logError("'To' SMS Email address invalid, or not specified"); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } else if (StringTools.isBlank(subj) && StringTools.isBlank(body)) { Print.logError("Command string not specified"); return DCServerFactory.ResultCode.INVALID_ARG; } else { try { Print.logInfo("SMS email: to <" + toEmail + ">"); Print.logDebug(" From : " + frEmail); Print.logDebug(" To : " + toEmail); Print.logDebug(" Subject: " + subj); Print.logDebug(" Message: " + body); SendMail.send(frEmail, toEmail, null, null, subj, body, null); return DCServerFactory.ResultCode.SUCCESS; } catch (Throwable t) { // NoClassDefFoundException, ClassNotFoundException // this will fail if JavaMail support for SendMail is not available. Print.logWarn("SendMail error: " + t); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } } }
private static void usage() { Print.logInfo("Usage:"); Print.logInfo(" java ... " + ClientSocketThread.class.getName() + " {options}"); Print.logInfo("'Send' Options:"); Print.logInfo(" -host=<host> The destination host"); Print.logInfo(" -port=<port> The destination port"); Print.logInfo(" -send=<data> The data to send (prefix with '0x' for hex data)"); Print.logInfo("'Receive' Options (not yet implemented):"); Print.logInfo(" -port=<port> The port on which to listen for incoming data"); Print.logInfo(" -recv Set to 'receive' mode"); System.exit(1); }
protected boolean _setDevice(Device device, String ipAddress, int clientPort) { /* valid device? */ if (device == null) { return false; } /* validate ID address */ DataTransport dataXPort = device.getDataTransport(); if ((ipAddress != null) && !dataXPort.isValidIPAddress(ipAddress)) { Print.logError( "Invalid IPAddr: " + device.getAccountID() + "/" + device.getDeviceID() + " Found=" + ipAddress + " Expect=" + dataXPort.getIpAddressValid()); return false; } /* update device */ this.device = device; this.dataXPort = dataXPort; this.dataXPort.setIpAddressCurrent(ipAddress); // FLD_ipAddressCurrent this.dataXPort.setRemotePortCurrent(clientPort); // FLD_remotePortCurrent this.dataXPort.setDeviceCode(this.getDeviceCode()); // FLD_deviceCode this.device.setLastTotalConnectTime(DateTime.getCurrentTimeSec()); // FLD_lastTotalConnectTime /* ok */ return true; }
protected String getStringProperty(Device device, String key, String dft) { DCServerConfig dcs = (device != null) ? DCServerFactory.getServerConfig(device.getDeviceCode()) : null; String prop = null; if (dcs != null) { prop = dcs.getStringProperty(key, dft); Print.logInfo("DCServerConfig property '" + key + "' ==> " + prop); if (StringTools.isBlank(prop) && RTConfig.hasProperty(key)) { Print.logInfo("(RTConfig property '" + key + "' ==> " + RTConfig.getString(key, "") + ")"); } } else { prop = RTConfig.getString(key, dft); Print.logInfo("RTConfig property '" + key + "' ==> " + prop); } return prop; }
/* private constructor */ private TrackServer(int tcpPorts[], int udpPorts[], int commandPort) throws Throwable { int listeners = 0; // Start TCP listeners if (!ListTools.isEmpty(tcpPorts)) { for (int i = 0; i < tcpPorts.length; i++) { int port = tcpPorts[i]; if (ServerSocketThread.isValidPort(port)) { try { this._startTCP(port); listeners++; } catch (java.net.BindException be) { Print.logError("TCP: Error binding to port: %d", port); } } else { throw new Exception("TCP: Invalid port number: " + port); } } } // Start UDP listeners if (!ListTools.isEmpty(udpPorts)) { for (int i = 0; i < udpPorts.length; i++) { int port = udpPorts[i]; if (ServerSocketThread.isValidPort(port)) { try { ServerSocketThread sst = this._startUDP(port); if (this.udpSocket == null) { this.udpSocket = sst.getDatagramSocket(); } listeners++; } catch (java.net.BindException be) { Print.logError("UDP: Error binding to port: %d", port); } } else { throw new Exception("UDP: Invalid port number: " + port); } } } /* do we have any active listeners? */ if (listeners <= 0) { Print.logWarn("No active device communication listeners!"); } }
public void run() { String command = null; Print.logInfo("Client:OutputThread started"); while (true) { /* wait for commands */ synchronized (this.cmdList) { while ((this.cmdList.size() <= 0) && (getRunStatus() == THREAD_RUNNING)) { try { this.cmdList.wait(5000L); } catch (Throwable t) { /*ignore*/ } } if (getRunStatus() != THREAD_RUNNING) { break; } command = this.cmdList.remove(0).toString(); } /* send commands */ try { ClientSocketThread.socketWriteLine(this.socket, command); } catch (Throwable t) { Print.logError("Client:OutputThread - " + t); t.printStackTrace(); break; } } if (getRunStatus() == THREAD_RUNNING) { Print.logWarn("Client:OutputThread stopped due to error"); } else { Print.logInfo("Client:OutputThread stopped"); } synchronized (this.threadLock) { this.isRunning = false; Print.logInfo("Client:OutputThread stopped"); this.threadLock.notify(); } }
protected Device loadDevice(String acctID, String devID) { if (StringTools.isBlank(acctID)) { return this.loadDevice(devID); // load ad ModemID } else { try { Account account = Account.getAccount(acctID); if (account == null) { Print.logError("Account-ID not found: " + acctID); return null; } else { Device dev = Transport.loadDeviceByTransportID(account, devID); return dev; } } catch (DBException dbe) { Print.logError("Error getting Device: " + acctID + "/" + devID + " [" + dbe + "]"); return null; } } }
/** ** Add SMS Gateway support provider */ public static void AddSMSGateway(String name, SMSOutboundGateway smsGW) { /* validate name */ if (StringTools.isBlank(name)) { Print.logWarn("SMS Gateway name is blank"); return; } else if (smsGW == null) { Print.logWarn("SMS Gateway handler is null"); return; } /* initialize map? */ if (SmsGatewayHandlerMap == null) { SmsGatewayHandlerMap = new HashMap<String, SMSOutboundGateway>(); } /* save handler */ SmsGatewayHandlerMap.put(name.toLowerCase(), smsGW); Print.logDebug("Added SMS Gateway Handler: " + name); }
private static void usage() { Print.logInfo("Usage:"); Print.logInfo(" java ... " + URIArg.class.getName() + " {options}"); Print.logInfo("Options:"); Print.logInfo(" -encode=<ASCII> Encode ASCII string to URL argument string"); Print.logInfo(" -decode=<args> Decode URL argument string to ASCII"); Print.logInfo(" -rtpEnc=<url> RTP Encode URL [key = 'rtp']"); Print.logInfo(" -rtpDec=<url> RTP Decode URL [key = 'rtp']"); System.exit(1); }
/** ** Descrambles String */ public static String _des64(String e) { if (!StringTools.isBlank(e)) { try { byte b[] = Base64.decode(e, getBase64Alphabet(), Base64Pad); return StringTools.toStringValue(b, ' '); } catch (Base64.Base64DecodeException bde) { Print.logError("Invalid Base64 characters", bde); return ""; } } else { return ""; } }
/** * ** Invokes all listeners with an assciated command ** @param r a string that may specify a * command (possibly one ** of several) associated with the event */ protected void invokeListeners(String r) { if (this.actionListeners != null) { for (Iterator i = this.actionListeners.iterator(); i.hasNext(); ) { ActionListener al = (ActionListener) i.next(); ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, r); try { al.actionPerformed(ae); } catch (Throwable t) { Print.logError("Exception: " + t.getMessage()); } } } }
/** ** Main entry point for testing/debugging ** @param argv Comand-line arguments */ public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); /* decode URI argument strings */ if (RTConfig.hasProperty(ARG_DECODE)) { String a = RTConfig.getString(ARG_DECODE, ""); String s = URIArg.decodeArg(new StringBuffer(), a).toString(); Print.sysPrintln("ASCII: " + s); System.exit(0); } /* encode Base64 strings */ if (RTConfig.hasProperty(ARG_ENCODE)) { String s = RTConfig.getString(ARG_ENCODE, ""); String a = URIArg.encodeArg(new StringBuffer(), s).toString(); Print.sysPrintln("Args: " + a); System.exit(0); } /* RTP decode */ if (RTConfig.hasProperty(ARG_RTPDEC)) { URIArg rtpUrl = new URIArg(RTConfig.getString(ARG_RTPDEC, "")); URIArg decUrl = rtpUrl.rtpDecode("rtp"); Print.sysPrintln("URL: " + decUrl.toString()); System.exit(0); } /* RTP encode */ if (RTConfig.hasProperty(ARG_RTPENC)) { URIArg decUrl = new URIArg(RTConfig.getString(ARG_RTPENC, "")); URIArg rtpUrl = decUrl.rtpEncode("rtp"); Print.sysPrintln("URL: " + rtpUrl.toString()); System.exit(0); } /* no options */ usage(); }
/* start TCP listener */ private void _startTCP(int port) throws Throwable { ServerSocketThread sst = null; /* create server socket */ try { sst = new ServerSocketThread(port); } catch (Throwable t) { // trap any server exception Print.logException("ServerSocket error", t); throw t; } /* initialize */ sst.setTextPackets(Constants.ASCII_PACKETS); sst.setBackspaceChar(null); // no backspaces allowed sst.setLineTerminatorChar(Constants.ASCII_LINE_TERMINATOR); sst.setIgnoreChar(Constants.ASCII_IGNORE_CHARS); sst.setMaximumPacketLength(Constants.MAX_PACKET_LENGTH); sst.setMinimumPacketLength(Constants.MIN_PACKET_LENGTH); sst.setIdleTimeout(TrackServer.tcpTimeout_idle); // time between packets sst.setPacketTimeout( TrackServer.tcpTimeout_packet); // time from start of packet to packet completion sst.setSessionTimeout(TrackServer.tcpTimeout_session); // time for entire session sst.setTerminateOnTimeout(Constants.TERMINATE_ON_TIMEOUT); sst.setClientPacketHandlerClass(TrackClientPacketHandler.class); sst.setLingerTimeoutSec(Constants.LINGER_ON_CLOSE_SEC); /* start thread */ Print.logInfo( "Starting TCP listener thread on port " + port + " [timeout=" + sst.getSessionTimeout() + "ms] ..."); sst.start(); this.tcpThread.add(sst); }
public boolean updateDevice() { if (this.device != null) { /* save device changes */ try { // TODO: check "this.device" vs "this.dataXPort" this.device.updateChangedEventFields(); return true; } catch (DBException dbe) { Print.logException( "Unable to update Device: " + this.getAccountID() + "/" + this.getDeviceID(), dbe); } finally { // } } return false; }
/** ** Main entry point for testing/debugging ** @param argv Comand-line arguments */ public static void main(String argv[]) { RTConfig.setCommandLineArgs(argv); String host = RTConfig.getString(ARG_HOST, null); int port = RTConfig.getInt(ARG_PORT, 0); /* send data */ if (RTConfig.hasProperty(ARG_SEND)) { if (StringTools.isBlank(host)) { Print.logError("Target host not specified"); usage(); } if (port <= 0) { Print.logError("Target port not specified"); usage(); } String dataStr = RTConfig.getString(ARG_SEND, "hello"); byte data[] = dataStr.startsWith("0x") ? StringTools.parseHex(dataStr, null) : dataStr.getBytes(); ClientSocketThread cst = new ClientSocketThread(host, port); try { cst.openSocket(); cst.socketWriteBytes(data); } catch (Throwable t) { Print.logException("Error", t); } finally { cst.closeSocket(); } System.exit(0); } /* receive data */ if (RTConfig.hasProperty(ARG_RECEIVE)) { if (port <= 0) { Print.logError("Target port not specified"); usage(); } if (!StringTools.isBlank(host)) { Print.logWarn("Specified 'host' will be ignored"); } Print.logError("Receive not yet implemented ..."); System.exit(99); } /* show usage */ usage(); }
public boolean insertEventData() { /* valid device? */ if (this.device == null) { return false; } /* debug message */ if (RTConfig.isDebugMode()) { Print.logDebug("Inserting EventData ...\n" + this.toString()); } /* EventData key */ String acctID = this.device.getAccountID(); String devID = this.device.getDeviceID(); long fixtime = this.getTimestamp(); int statusCode = this.getStatusCode(); EventData.Key evKey = new EventData.Key(acctID, devID, fixtime, statusCode); EventData evdb = evKey.getDBRecord(); /* set EventData field values */ if (USE_EVENTDATA_SETVALUE) { for (Object fldn : this.fieldValues.getPropertyKeys()) { if (fldn.equals(EventData.FLD_timestamp)) { continue; // already set above } else if (fldn.equals(EventData.FLD_statusCode)) { continue; // already set above } Object fldv = this.fieldValues.getProperty(fldn, null); if (fldv != null) { evdb.setValue((String) fldn, fldv); // attempts to use "setter" methods } } } else { if (this.hasLatitude()) { evdb.setLatitude(this.getLatitude()); } if (this.hasLongitude()) { evdb.setLongitude(this.getLongitude()); } if (this.hasGpsAge()) { evdb.setGpsAge(this.getGpsAge()); } if (this.hasHDOP()) { evdb.setHDOP(this.getHDOP()); } if (this.hasSatelliteCount()) { evdb.setSatelliteCount(this.getSatelliteCount()); } if (this.hasSpeedKPH()) { evdb.setSpeedKPH(this.getSpeedKPH()); } if (this.hasHeading()) { evdb.setHeading(this.getHeading()); } if (this.hasAltitude()) { evdb.setAltitude(this.getAltitude()); } if (this.hasInputMask()) { evdb.setInputMask(this.getInputMask()); } if (this.hasBatteryLevel()) { evdb.setBatteryLevel(this.getBatteryLevel()); } if (this.hasSignalStrength()) { evdb.setSignalStrength(this.getSignalStrength()); } if (this.hasOdometerKM()) { evdb.setOdometerKM(this.getOdometerKM()); } if (this.hasEngineHours()) { evdb.setEngineHours(this.getEngineHours()); } if (this.hasPtoHours()) { evdb.setPtoHours(this.getPtoHours()); } if (this.hasFuelTotal()) { evdb.setFuelTotal(this.getFuelTotal()); } if (this.hasGeozoneID()) { evdb.setGeozoneID(this.getGeozoneID()); } } /* other fields (if available) */ if (this.otherValues != null) { for (String fldn : this.otherValues.keySet()) { if (fldn.equals(EventData.FLD_timestamp)) { continue; } else if (fldn.equals(EventData.FLD_statusCode)) { continue; } Object fldv = this.otherValues.get(fldn); if (fldv != null) { evdb.setValue(fldn, fldv); // attempts to use "setter" methods } } } /* insert event */ // this will display an error if it was unable to store the event Print.logInfo( "Event : [0x" + StringTools.toHexString(statusCode, 16) + "] " + StatusCodes.GetDescription(statusCode, null)); this.device.insertEventData( evdb); // FLD_lastValidLatitude,FLD_lastValidLongitude,FLD_lastGPSTimestamp,FLD_lastOdometerKM this.eventTotalCount++; return true; }
/** ** Initialize outbound SMS gateway handlers */ public static void _startupInit() { Print.logDebug("SMSOutboundGateway initializing ..."); final String SMSKey_ = SMSOutboundGateway.PROP_SmsGatewayHandler_; /* already initialized? */ if (SmsGatewayHandlerMap != null) { return; } // ----------------------------------------------- // The following shows several example of outbound SMS gateway support. // The only method that needs to be overridden and implemented is // public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) // The "device" is the Device record instance to which the SMS message should be sent, // and "commandStr" is the SMS text (device command) which is to be sent to the device. // ----------------------------------------------- /* standard "Body" command */ // Property: // [email protected] // Notes: // This outbound SMS method sends the SMS text in an email message body to the device // "smsEmailAddress". If the device "smsEmailAddress" is blank, then the "To" email // address is constructed from the device "simPhoneNumber" and the email address // specified on the property "SmsGatewayHandler.emailBody.smsEmailAddress". SMSOutboundGateway.AddSMSGateway( "emailBody", new SMSOutboundGateway() { public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) { if (device == null) { return DCServerFactory.ResultCode.INVALID_DEVICE; } String frEmail = this.getFromEmailAddress(device); String toEmail = this.getSmsEmailAddress(device); if (StringTools.isBlank(toEmail)) { String smsEmail = this.getStringProperty(device, SMSKey_ + "emailBody.smsEmailAddress", ""); toEmail = smsEmail.startsWith("@") ? (device.getSimPhoneNumber() + smsEmail) : smsEmail; } return this.sendEmail(frEmail, toEmail, "", commandStr); } }); /* standard "Subject" command */ // Property: // SmsGatewayHandler.emailSubject.smsEmailAddress= // Notes: // This outbound SMS method sends the SMS text in an email message subject to the device // "smsEmailAddress". If the device "smsEmailAddress" is blank, then the "To" email // address is constructed from the device "simPhoneNumber" and the email address // specified on the property "SmsGatewayHandler.emailSubject.smsEmailAddress". SMSOutboundGateway.AddSMSGateway( "emailSubject", new SMSOutboundGateway() { public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) { if (device == null) { return DCServerFactory.ResultCode.INVALID_DEVICE; } String frEmail = this.getFromEmailAddress(device); String toEmail = this.getSmsEmailAddress(device); if (StringTools.isBlank(toEmail)) { String smsEmail = this.getStringProperty(device, SMSKey_ + "emailSubject.smsEmailAddress", ""); toEmail = smsEmail.startsWith("@") ? (device.getSimPhoneNumber() + smsEmail) : smsEmail; } return this.sendEmail(frEmail, toEmail, commandStr, ""); } }); /* HTTP SMS */ // Property: // // SmsGatewayHandler.httpURL.url=http://localhost:12345/smsredirector/sendsms?flash=0&acctuser=user&tracking_Pwd=pass&source=5551212&destination=${mobile}&message=${message} // // SmsGatewayHandler.httpURL.url=http://localhost:12345/sendsms?user=user&pass=pass&source=5551212&dest=${mobile}&text=${message} // Notes: // This outbound SMS method sends the SMS text in an HTTP "GET" request to the URL // specified on the property "SmsGatewayHandler.httpURL.url". The following replacement // variables may be specified in the URL string: // ${mobile} - replaced with the Device "simPhoneNumber" field contents // ${message} - replaced with the SMS text/command to be sent to the device. // It is expected that the server handling the request understands how to parse and // interpret the various fields in the URL. SMSOutboundGateway.AddSMSGateway( "httpURL", new SMSOutboundGateway() { public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) { if (device == null) { return DCServerFactory.ResultCode.INVALID_DEVICE; } String KeyURL = SMSKey_ + "httpURL.url"; String mobile = URIArg.encodeArg(device.getSimPhoneNumber()); String message = URIArg.encodeArg(commandStr); String httpURL = this.getStringProperty(device, KeyURL, ""); if (StringTools.isBlank(httpURL)) { Print.logWarn("'" + KeyURL + "' not specified"); return DCServerFactory.ResultCode.INVALID_SMS; } httpURL = StringTools.replace(httpURL, "${mobile}", mobile); httpURL = StringTools.replace(httpURL, "${message}", message); try { Print.logDebug("SMS Gateway URL: " + httpURL); byte response[] = HTMLTools.readPage_GET(httpURL, 10000); // TODO: check response? return DCServerFactory.ResultCode.SUCCESS; } catch (UnsupportedEncodingException uee) { Print.logError("URL Encoding: " + uee); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } catch (NoRouteToHostException nrthe) { Print.logError("Unreachable Host: " + httpURL); return DCServerFactory.ResultCode.UNKNOWN_HOST; } catch (UnknownHostException uhe) { Print.logError("Unknown Host: " + httpURL); return DCServerFactory.ResultCode.UNKNOWN_HOST; } catch (FileNotFoundException fnfe) { Print.logError("Invalid URL (not found): " + httpURL); return DCServerFactory.ResultCode.INVALID_SMS; } catch (MalformedURLException mue) { Print.logError("Invalid URL (malformed): " + httpURL); return DCServerFactory.ResultCode.INVALID_SMS; } catch (Throwable th) { Print.logError("HTML SMS error: " + th); return DCServerFactory.ResultCode.TRANSMIT_FAIL; } } }); }
/** ** Called when the thread has stopped */ protected void threadStopped() { Print.logDebug("Client:ControlThread stopped ..."); }