-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract framing from RelpFrame to FrameClock (#186)
* extract framing from RelpFrame to FrameClock * give private methods of RelpReadImpl better names: innerLoop -> attemptFrameCompletion, processFrame -> delegateFrame * remove left over comment
- Loading branch information
Showing
6 changed files
with
253 additions
and
99 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
123 changes: 123 additions & 0 deletions
123
src/main/java/com/teragrep/rlp_03/frame/FrameClock.java
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,123 @@ | ||
/* | ||
* Java Reliable Event Logging Protocol Library Server Implementation RLP-03 | ||
* Copyright (C) 2021-2024 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.rlp_03.frame; | ||
|
||
import com.teragrep.rlp_03.frame.fragment.Fragment; | ||
import com.teragrep.rlp_03.frame.fragment.FragmentImpl; | ||
import com.teragrep.rlp_03.frame.fragment.FragmentStub; | ||
import com.teragrep.rlp_03.frame.function.*; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class FrameClock { | ||
|
||
private final RelpFrame relpFrameStub; | ||
|
||
private Fragment txn; | ||
private Fragment command; | ||
private Fragment payloadLength; | ||
private Fragment payload; | ||
private Fragment endOfTransfer; | ||
|
||
public FrameClock() { | ||
this.relpFrameStub = new RelpFrameStub(); | ||
clear(); | ||
} | ||
|
||
private void clear() { | ||
this.txn = new FragmentImpl(new TransactionFunction()); | ||
this.command = new FragmentImpl(new CommandFunction()); | ||
this.payloadLength = new FragmentImpl(new PayloadLengthFunction()); | ||
this.payload = new FragmentStub(); | ||
this.endOfTransfer = new FragmentImpl(new EndOfTransferFunction()); | ||
} | ||
|
||
public synchronized RelpFrame submit(ByteBuffer input) { | ||
boolean ready = false; | ||
|
||
while (input.hasRemaining() && !ready) { | ||
|
||
if (!txn.isComplete()) { | ||
txn.accept(input); | ||
} | ||
else if (!command.isComplete()) { | ||
command.accept(input); | ||
} | ||
else if (!payloadLength.isComplete()) { | ||
payloadLength.accept(input); | ||
|
||
if (payloadLength.isComplete()) { | ||
// PayloadFunction depends on payload length and needs to by dynamically created | ||
int payloadSize = payloadLength.toInt(); | ||
payload = new FragmentImpl(new PayloadFunction(payloadSize)); | ||
} | ||
} | ||
else if (!payload.isComplete()) { | ||
payload.accept(input); | ||
} | ||
else if (!endOfTransfer.isComplete()) { | ||
endOfTransfer.accept(input); | ||
|
||
if (endOfTransfer.isComplete()) { | ||
// all complete | ||
ready = true; | ||
} | ||
} | ||
else { | ||
throw new IllegalStateException("submit not allowed on a complete frame"); | ||
} | ||
} | ||
|
||
if (ready) { | ||
RelpFrame relpFrame = new RelpFrameImpl(txn, command, payloadLength, payload, endOfTransfer); | ||
clear(); | ||
return relpFrame; | ||
} | ||
else { | ||
return relpFrameStub; | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/com/teragrep/rlp_03/frame/FrameClockLeaseful.java
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,83 @@ | ||
/* | ||
* Java Reliable Event Logging Protocol Library Server Implementation RLP-03 | ||
* Copyright (C) 2021-2024 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.rlp_03.frame; | ||
|
||
import com.teragrep.rlp_03.channel.buffer.BufferLease; | ||
import com.teragrep.rlp_03.channel.buffer.BufferLeasePool; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class FrameClockLeaseful { | ||
|
||
private final BufferLeasePool bufferLeasePool; | ||
private final FrameClock frameClock; | ||
private final List<BufferLease> leases; | ||
|
||
public FrameClockLeaseful(BufferLeasePool bufferLeasePool, FrameClock frameClock) { | ||
this.bufferLeasePool = bufferLeasePool; | ||
this.frameClock = frameClock; | ||
this.leases = new ArrayList<>(); | ||
} | ||
|
||
public RelpFrame submit(BufferLease bufferLease) { | ||
leases.add(bufferLease); | ||
RelpFrame relpFrame = frameClock.submit(bufferLease.buffer()); | ||
|
||
RelpFrame rv; | ||
if (relpFrame.isStub()) { | ||
rv = new RelpFrameLeaseful(bufferLeasePool, relpFrame, Collections.emptyList()); | ||
} | ||
else { | ||
LinkedList<BufferLease> frameLeases = new LinkedList<>(leases); | ||
rv = new RelpFrameLeaseful(bufferLeasePool, relpFrame, frameLeases); | ||
leases.clear(); | ||
} | ||
return rv; | ||
} | ||
} |
Oops, something went wrong.