Skip to content

Commit 38a879a

Browse files
v1.1.0 Release
IPC連携用のパラレルクラスの導入
1 parent 83d2bd4 commit 38a879a

5 files changed

Lines changed: 173 additions & 6 deletions

File tree

app/MainClass/MainForEventHandlerSample.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use App\ContextClass\ContextForSample;
1111
use App\EventClass\EventHandlerSample;
12+
use App\ParallelClass\ParallelForSimpleSocket;
1213

1314

1415
/**
@@ -42,8 +43,9 @@ class MainForEventHandlerSample extends MainForRestApi
4243
* @var array $classes 設定クラス群
4344
*/
4445
protected array $classes = [
45-
'context' => ContextForSample::class,
46-
'event' => EventHandlerSample::class
46+
'context' => ContextForSample::class,
47+
'event' => EventHandlerSample::class,
48+
'parallel' => ParallelForSimpleSocket::class
4749
];
4850

4951
/**

app/MainClass/MainForRestApi.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\UnitParameter\ParameterForRestApi;
1515
use App\ProtocolUnits\ProtocolForRestApi;
1616
use App\CommandUnits\CommandForRestApi;
17+
use App\ParallelClass\IParallelClass;
1718

1819

1920
/**
@@ -47,15 +48,21 @@ class MainForRestApi extends Console
4748
* @var array $classes 設定クラス群
4849
*/
4950
protected array $classes = [
50-
'context' => ParameterForRestApi::class,
51-
'event' => CommandForRestApi::class
51+
'context' => ParameterForRestApi::class,
52+
'event' => CommandForRestApi::class,
53+
'parallel' => null
5254
];
5355

5456
/**
5557
* @var int $port ポート番号
5658
*/
5759
protected int $port;
5860

61+
/**
62+
* @var ?IParallelClass $parallel パラレルインターフェースのインスタンス
63+
*/
64+
protected ?IParallelClass $parallel = null;
65+
5966

6067
/**
6168
* 設定ファイル検証
@@ -302,6 +309,13 @@ public function exec()
302309
goto finish; // リッスン失敗
303310
}
304311

312+
// パラレルクラスの初期化処理
313+
if($this->classes['parallel'] !== null)
314+
{
315+
$this->parallel = new $this->classes['parallel']($unit_parameter);
316+
$this->parallel->initMain();
317+
}
318+
305319
$cycle_interval = $cli_parameter['cycle_interval'];
306320
$alive_interval = $cli_parameter['alive_interval'];
307321

@@ -314,6 +328,16 @@ public function exec()
314328
{
315329
goto finish;
316330
}
331+
332+
// 周期ドリブン(パラレルクラス用)
333+
if($this->parallel)
334+
{
335+
$this->parallel->cycleDriven($cycle_interval, $alive_interval);
336+
if($ret === false)
337+
{
338+
goto finish;
339+
}
340+
}
317341
}
318342

319343
finish:

app/MainClass/MainForStateMachineSample.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use App\ContextClass\ContextForSample;
1111
use App\EventClass\StateMachineSample;
12+
use App\ParallelClass\ParallelForSimpleSocket;
1213

1314

1415
/**
@@ -42,8 +43,9 @@ class MainForStateMachineSample extends MainForRestApi
4243
* @var array $classes 設定クラス群
4344
*/
4445
protected array $classes = [
45-
'context' => ContextForSample::class,
46-
'event' => StateMachineSample::class
46+
'context' => ContextForSample::class,
47+
'event' => StateMachineSample::class,
48+
'parallel' => ParallelForSimpleSocket::class
4749
];
4850

4951
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* パラレルクラスのインタフェースファイル
4+
*
5+
* REST-APIサーバーのメイン処理クラスへ渡すためのインターフェースファイル
6+
*/
7+
8+
namespace App\ParallelClass;
9+
10+
11+
/**
12+
* パラレルクラスのインタフェース
13+
*
14+
* REST-APIサーバーのメイン処理クラスへ渡すためのインターフェース定義
15+
*/
16+
interface IParallelClass
17+
{
18+
/**
19+
* メイン処理の初期化処理
20+
*
21+
* 初期化処理の依存性注入
22+
*
23+
* @return bool true(成功) or false(失敗)
24+
*/
25+
public function initMain(): bool;
26+
27+
/**
28+
* 周期ドリブン処理
29+
*
30+
* イベントループへの依存性注入
31+
*
32+
* @param int $p_cycle_interval REST-APIの周期インターバルタイム(マイクロ秒)
33+
* @param int $p_alive_interval REST-APIのアライブチェックインターバルタイム(秒)
34+
* @return bool true(成功) or false(失敗)
35+
*/
36+
public function cycleDriven(int $p_cycle_interval, int $p_alive_interval): bool;
37+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* シンプルソケットのパラレルクラスのファイル
4+
*
5+
* REST-APIサーバーのメイン処理クラスへ渡すためのクラスファイル
6+
*/
7+
8+
namespace App\ParallelClass;
9+
10+
use SocketManager\Library\SimpleSocketGenerator;
11+
use SocketManager\Library\SimpleSocketTypeEnum;
12+
use SocketManager\Library\ISimpleSocketUdp;
13+
14+
use App\ContextClass\ContextForSample;
15+
16+
17+
/**
18+
* シンプルソケットのパラレルクラス
19+
*
20+
* REST-APIサーバーのメイン処理クラスへ渡すためのクラス
21+
*/
22+
class ParallelForSimpleSocket implements IParallelClass
23+
{
24+
private ContextForSample $context;
25+
private SimpleSocketGenerator $generator;
26+
27+
/**
28+
* コンストラクタ
29+
*
30+
* @param ContextForSample $p_context コンテキストクラスのインスタンス
31+
*/
32+
public function __construct($p_context)
33+
{
34+
$this->context = $p_context;
35+
}
36+
37+
/**
38+
* メイン処理の初期化処理
39+
*
40+
* 初期化処理の依存性注入
41+
*
42+
* @return bool true(成功) or false(失敗)
43+
*/
44+
public function initMain(): bool
45+
{
46+
$this->generator = new SimpleSocketGenerator(SimpleSocketTypeEnum::UDP, null, null, 1000);
47+
$this->generator->setKeepRunning(function(?ISimpleSocketUdp $p_simple_socket, ContextForSample $p_context)
48+
{
49+
$cids = $p_context->getConnectionIdAll();
50+
$all_cnt = count($cids);
51+
$send_data =
52+
[
53+
[
54+
'service' => 'minecraft-parent',
55+
'type' => 'user-cnt',
56+
'data' => $all_cnt
57+
]
58+
];
59+
$serialize_data = json_encode($send_data);
60+
$w_ret = $p_simple_socket->sendto('localhost', 15000, $serialize_data);
61+
if($w_ret === null)
62+
{
63+
return;
64+
}
65+
else
66+
if($w_ret === false)
67+
{
68+
return;
69+
}
70+
}, $this->context);
71+
72+
$w_ret = $this->generator->generate();
73+
if($w_ret === null)
74+
{
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
81+
/**
82+
* 周期ドリブン処理
83+
*
84+
* イベントループへの依存性注入
85+
*
86+
* @param int $p_cycle_interval REST-APIの周期インターバルタイム(マイクロ秒)
87+
* @param int $p_alive_interval REST-APIのアライブチェックインターバルタイム(秒)
88+
* @return bool true(成功) or false(失敗)
89+
*/
90+
public function cycleDriven(int $p_cycle_interval, int $p_alive_interval): bool
91+
{
92+
// 周期ドリブン
93+
$ret = $this->generator->cycleDriven($p_cycle_interval);
94+
if($ret === false)
95+
{
96+
$this->generator->shutdownAll();
97+
return false;
98+
}
99+
100+
return true;
101+
}
102+
}

0 commit comments

Comments
 (0)