Skip to content

Commit

Permalink
Added DaemonRpcClient::setLimit()
Browse files Browse the repository at this point in the history
  • Loading branch information
refring committed Oct 4, 2023
1 parent 5efc4c0 commit 30fc606
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 5 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ After that, run `composer test:integration` to run the integration tests.
- [ ] /stop_daemon
- [ ] /get_info (not JSON)
- [ ] /get_limit
- [ ] /set_limit
- [ ] /out_peers
- [ ] /in_peers
- [ ] /start_save_graph
- [ ] /stop_save_graph
- [ ] /get_outs
- [ ] /update

Expand Down
37 changes: 37 additions & 0 deletions src/DaemonOther/SetLimitRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace RefRing\MoneroRpcPhp\DaemonOther;

use RefRing\MoneroRpcPhp\Request\OtherRpcRequest;
use Square\Pjson\Json;
use Square\Pjson\JsonSerialize;

/**
* Set daemon bandwidth limits.
*/
class SetLimitRequest extends OtherRpcRequest
{
use JsonSerialize;

/**
* Download limit in kBytes per second (-1 reset to default, 0 don't change the current limit)
*/
#[Json('limit_down', omit_empty: true)]
public ?int $limitDown;

/**
* Upload limit in kBytes per second (-1 reset to default, 0 don't change the current limit)
*/
#[Json('limit_up', omit_empty: true)]
public ?int $limitUp;

public static function create(?int $limitDown = null, ?int $limitUp = null): OtherRpcRequest
{
$self = new self();
$self->limitDown = $limitDown;
$self->limitUp = $limitUp;
return $self;
}
}
29 changes: 29 additions & 0 deletions src/DaemonOther/SetLimitResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace RefRing\MoneroRpcPhp\DaemonOther;

use RefRing\MoneroRpcPhp\DaemonRpc\DaemonBaseResponse;
use Square\Pjson\Json;
use Square\Pjson\JsonSerialize;

/**
* Set daemon bandwidth limits.
*/
class SetLimitResponse extends DaemonBaseResponse
{
use JsonSerialize;

/**
* Download limit in kBytes per second
*/
#[Json('limit_down')]
public int $limitDown;

/**
* Upload limit in kBytes per second
*/
#[Json('limit_up')]
public int $limitUp;
}
1 change: 0 additions & 1 deletion src/DaemonOther/SetLogHashRateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace RefRing\MoneroRpcPhp\DaemonOther;

use RefRing\MoneroRpcPhp\DaemonRpc\DaemonBaseResponse;
use Square\Pjson\Json;
use Square\Pjson\JsonSerialize;

/**
Expand Down
1 change: 0 additions & 1 deletion src/DaemonOther/SetLogLevelResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace RefRing\MoneroRpcPhp\DaemonOther;

use RefRing\MoneroRpcPhp\DaemonRpc\DaemonBaseResponse;
use Square\Pjson\Json;
use Square\Pjson\JsonSerialize;

/**
Expand Down
15 changes: 15 additions & 0 deletions src/DaemonRpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use RefRing\MoneroRpcPhp\DaemonOther\SaveBlockchainResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SendRawTransactionRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SendRawTransactionResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLimitRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SetLimitResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogCategoriesRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogCategoriesResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogHashRateRequest;
Expand Down Expand Up @@ -641,4 +643,17 @@ public function setLogCategories(?string $categories = null): SetLogCategoriesRe
$this->endPointPath = '/set_log_categories';
return $this->handleRequest(SetLogCategoriesRequest::create($categories), SetLogCategoriesResponse::class);
}

/**
* Set daemon bandwidth limits.
*
* @param int $limitDown Download limit in kBytes per second (-1 reset to default, 0 don't change the current limit)
* @param int $limitUp Upload limit in kBytes per second (-1 reset to default, 0 don't change the current limit)
* @throws MoneroRpcException
*/
public function setLimit(int $limitDown, int $limitUp): SetLimitResponse
{
$this->endPointPath = '/set_limit';
return $this->handleRequest(SetLimitRequest::create($limitDown, $limitUp), SetLimitResponse::class);
}
}
9 changes: 9 additions & 0 deletions tests/unit/DaemonOtherDeserializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RefRing\MoneroRpcPhp\DaemonOther\PopBlocksResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SaveBlockchainResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SendRawTransactionResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLimitResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogCategoriesResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogHashRateResponse;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogLevelResponse;
Expand Down Expand Up @@ -131,6 +132,14 @@ public function testSetLogCategories()
$this->assertSame($responseFlat, $response->toJson());
}

public function testSetLimit()
{
$jsonResponse = '{"limit_down":1024,"limit_up":128,"status":"OK","untrusted":false}';
$response = SetLimitResponse::fromJsonString($jsonResponse);
$responseFlat = $this->comparableJson($jsonResponse);
$this->assertSame($responseFlat, $response->toJson());
}

public static function comparableJson(string $json): string
{
return json_encode(json_decode($json));
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/DaemonOtherSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use RefRing\MoneroRpcPhp\DaemonOther\PopBlocksRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SaveBlockchainRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SendRawTransactionRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SetLimitRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogCategoriesRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogHashRateRequest;
use RefRing\MoneroRpcPhp\DaemonOther\SetLogLevelRequest;
Expand Down Expand Up @@ -113,4 +114,11 @@ public function testSetLogCategories()
$request = SetLogCategoriesRequest::create("*:INFO");
$this->assertSame($expected, $request->toJson());
}

public function testSetLimit()
{
$expected = '{"limit_down":1024}';
$request = SetLimitRequest::create(1024);
$this->assertSame($expected, $request->toJson());
}
}

0 comments on commit 30fc606

Please sign in to comment.