📄 dmtpclientpackethandler.java
字号:
private AccountID getAccountId() throws PacketParseException { if (this.accountId == null) { // throw termination error if the account hasn't been defined PacketParseException ppe = new PacketParseException(ServerErrors.NAK_ACCOUNT_INVALID, null); // errData ok ppe.setTerminate(); throw ppe; } return this.accountId; } // ------------------------------------------------------------------------ /* load device id */ private void loadDeviceId(String ipAddr, String devName) throws PacketParseException { /* device already defined? */ if (this.deviceId != null) { Print.logError("Device ID already defined"); throw new PacketParseException(ServerErrors.NAK_PROTOCOL_ERROR, null); // errData ok } else if ((devName == null) || devName.equals("")) { Print.logError("Device name is null/empty"); throw new PacketParseException(ServerErrors.NAK_DEVICE_INVALID, null); // errData ok } /* load device */ DeviceID devId = DeviceID.loadDeviceID(this.getAccountId(), devName.toLowerCase()); // will throw PacketParseException if named DeviceID does not exist /* set device */ this._setDeviceId(ipAddr, devId); } private void _setDeviceId(String ipAddr, DeviceID devId) throws PacketParseException { /* null? */ if (devId == null) { Print.logError("Device ID is null"); throw new PacketParseException(ServerErrors.NAK_DEVICE_INVALID, null); // errData ok } /* validate IP address */ if (!devId.isValidIpAddress(ipAddr)) { // invalid IP address Print.logError("Invalid IP address: " + ipAddr); throw new PacketParseException(ServerErrors.NAK_DEVICE_ERROR, null); // errData ok } /* validate device */ if (!devId.isActive()) { // device is not active Print.logError("Device is inactive: " + devId.getDeviceName()); throw new PacketParseException(ServerErrors.NAK_DEVICE_INACTIVE, null); // errData ok } else if (!devId.markAndValidateConnection(this.isDuplex())) { // DMT service provider refuses connection Print.logError("Excessive connections: " + devId.getDeviceName()); throw new PacketParseException(ServerErrors.NAK_EXCESSIVE_CONNECTIONS, null); // errData ok } /* save marked connection info */ if (SAVE_MARKED_CONNECTION_INFO) { int servErr = devId.saveChanges(); if (servErr != ServerErrors.NAK_OK) { throw new PacketParseException(servErr, null); // errData ok } } /**/ this.deviceId = devId; } private DeviceID getDeviceId() throws PacketParseException { if (this.deviceId == null) { // throw termination error if the device hasn't been defined PacketParseException ppe = new PacketParseException(ServerErrors.NAK_DEVICE_INVALID, null); // errData ok ppe.setTerminate(); throw ppe; } return this.deviceId; } // ------------------------------------------------------------------------ private Packet[] _handleError(Packet packet) throws PacketParseException { Payload payload = packet.getPayload(true); int errCode = (int)payload.readULong(2, 0L); /* handle specific error */ switch (errCode) { case ClientErrors.ERROR_PACKET_HEADER: case ClientErrors.ERROR_PACKET_TYPE: case ClientErrors.ERROR_PACKET_LENGTH: { // we must have sent the client an invalid packet return null; } case ClientErrors.ERROR_PACKET_ENCODING: { // only occurs if client does not support CSV encoding this.getDeviceId().removeEncoding(this.encoding); if (Encoding.IsEncodingAscii(this.encoding)) { this.encoding = Encoding.IsEncodingChecksum(this.encoding)? Encoding.ENCODING_BASE64_CKSUM : Encoding.ENCODING_BASE64; } else { // ignore this } return null; } case ClientErrors.ERROR_PACKET_PAYLOAD: { // we must have sent the client an invalid packet return null; } case ClientErrors.ERROR_PACKET_CHECKSUM: { // should try again? return null; } case ClientErrors.ERROR_PACKET_ACK: { // we must have sent the client an ack for a sequence it doesn't have return null; } case ClientErrors.ERROR_PROTOCOL_ERROR: { // we must have sent the client an invalid packet return null; } case ClientErrors.ERROR_PROPERTY_READ_ONLY: { // just note this somewhere return null; } case ClientErrors.ERROR_PROPERTY_WRITE_ONLY: { // just note this somewhere return null; } case ClientErrors.ERROR_PROPERTY_INVALID_ID: { // just note this somewhere return null; } case ClientErrors.ERROR_PROPERTY_INVALID_VALUE: { // just note this somewhere return null; } case ClientErrors.ERROR_PROPERTY_UNKNOWN_ERROR: { // ignore this, since we don't know what to do about this anyway return null; } case ClientErrors.ERROR_COMMAND_INVALID: case ClientErrors.ERROR_COMMAND_ERROR: { // ignore this, since we don't know what to do about this anyway return null; } case ClientErrors.ERROR_UPLOAD_TYPE: case ClientErrors.ERROR_UPLOAD_LENGTH: case ClientErrors.ERROR_UPLOAD_OFFSET_OVERLAP: case ClientErrors.ERROR_UPLOAD_OFFSET_GAP: case ClientErrors.ERROR_UPLOAD_OFFSET_OVERFLOW: case ClientErrors.ERROR_UPLOAD_FILE_NAME: case ClientErrors.ERROR_UPLOAD_CHECKSUM: case ClientErrors.ERROR_UPLOAD_SAVE: { // we should abort the upload here return null; } case ClientErrors.ERROR_GPS_EXPIRED: case ClientErrors.ERROR_GPS_FAILURE: { // pass these along to account owner return null; } default: { // pass these along to account owner return null; } } } // ------------------------------------------------------------------------ private int _handleEvent(Event event) throws PacketParseException { DeviceID devId = this.getDeviceId(); if (Print.isDebugLoggingLevel()) { //opendmtp/mobile Event: // IPAddress : 127.0.0.1 // RawData : $E073:F112<...>75 // StatusCode : [0xF112] InMotion // Timestamp : [1173601234] Sun Mar 11 14:25:17 CDT 2007 // GPSAge : [0x0004] 4 // GeoPoint : 36.01234/-142.12345 // SpeedKPH : [103.9 kph] 64.6 mph // Heading : 126.72 // AltitudeM : [1319.0 meters] 4327.4 feet // Sequence : [0x0075] 117 // SeqLen : [0x0001] 1 DMTPGeoEvent gev = event.getGeoEvent(); StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append(devId.getAccountName()).append("/").append(devId.getDeviceName()); sb.append(" "); sb.append(event.toString()); Print.logDebug(sb.toString()); } return devId.saveEvent(event); } // ------------------------------------------------------------------------ private void _clearPendingPackets() throws PacketParseException { if (this.pendingPackets != null) { Print.logInfo("Deleting sent PendingPackets ..."); this.getDeviceId().clearPendingPackets(this.pendingPackets); this.pendingPackets = null; } } private Packet[] _handlePacket(String ipAddr, Packet packet) throws PacketParseException { /* make sure we have a defined device */ if (!packet.isIdentType()) { // we must have a Device ID for anything other that an identification // type packet. The following attempts to get the device id and throws // an exception if the device has not yet been defined. this.getDeviceId(); } /* handle event packets separately */ if (packet.isEventType()) { /* create Event */ Event evData = null; try { evData = new Event(ipAddr, packet); this.eventTotalCount++; // count total events this.eventBlockCount++; // count event in this block } catch (PacketParseException ppe) { // NAK_FORMAT_NOT_RECOGNIZED? throw ppe; } /* check for errors */ if (this.eventErrorPacket == null) { // no errors received during this block, so far int err = this._handleEvent(evData); if (err == ServerErrors.NAK_OK) { // this event insertion was successful this.lastValidEvent = evData; } else if (err == ServerErrors.NAK_DUPLICATE_EVENT) { // this record already exists (not a critical error) // duplicate events are quietly ignored this.lastValidEvent = evData; } else { // A critical error occurred inserting this event. // One of the following: // ServerErrors.NAK_EXCESSIVE_EVENTS // ServerErrors.NAK_EVENT_ERROR Print.logError("Event insertion [" + StringTools.toHexString(err,16) + "] " + ServerErrors.getErrorDescription(err)); long seq = evData.getSequence(); int seqLen = evData.getSequenceLength(); PacketParseException ppe = null; if ((seq >= 0L) && (seqLen > 0)) { Payload p = new Payload(); p.writeULong(seq, seqLen); byte errData[] = p.getBytes(); ppe = new PacketParseException(err, packet, errData); // sequence } else { ppe = new PacketParseException(err, packet); // errData ok } this.eventErrorPacket = ppe.createServerErrorPacket(); } } else { // ignore this event }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -