-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add changes to ergo-core readme
- Loading branch information
Showing
4 changed files
with
76 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
ergo-core/src/main/scala/org/ergoplatform/network/message/MessageBase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.ergoplatform.network.message | ||
|
||
import org.ergoplatform.network.message.MessageConstants._ | ||
|
||
import scala.util.{Success, Try} | ||
|
||
/** | ||
* Trait for a ergo network message | ||
* | ||
* @param spec - message specification | ||
* @param input - message being wrapped, whether in byte-array form (if from outside), | ||
* or structured data (if formed locally) | ||
* @tparam Content - message data type | ||
*/ | ||
trait MessageBase[Content] { | ||
val spec: MessageSpec[Content] | ||
val input: Either[Array[Byte], Content] | ||
|
||
/** | ||
* Message data bytes | ||
*/ | ||
lazy val dataBytes: Array[Byte] = input match { | ||
case Left(db) => db | ||
case Right(d) => spec.toBytes(d) | ||
} | ||
|
||
/** | ||
* Structured message content | ||
*/ | ||
lazy val data: Try[Content] = input match { | ||
case Left(db) => spec.parseBytesTry(db) | ||
case Right(d) => Success(d) | ||
} | ||
|
||
lazy val dataLength: Int = dataBytes.length | ||
|
||
/** | ||
* @return serialized message length in bytes | ||
*/ | ||
def messageLength: Int = { | ||
if (dataLength > 0) { | ||
HeaderLength + ChecksumLength + dataLength | ||
} else { | ||
HeaderLength | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters