Skip to content

Commit

Permalink
Refactor packet layers from database into code
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 31, 2019
1 parent 2f959a9 commit 8570f36
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 350 deletions.
4 changes: 2 additions & 2 deletions src/controllers/Packet/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace BNETDocs\Controllers\Packet;

use \BNETDocs\Libraries\Packet;
use \BNETDocs\Libraries\PacketApplicationLayer as PktAppLayer;
use \BNETDocs\Libraries\Packet\Application as ApplicationLayer;
use \BNETDocs\Models\Packet\Index as PacketIndexModel;
use \BNETDocs\Views\Packet\IndexHtml as PacketIndexHtmlView;
use \BNETDocs\Views\Packet\IndexJSON as PacketIndexJSONView;
Expand Down Expand Up @@ -62,7 +62,7 @@ public function &run( Router &$router, View &$view, array &$args ) {
$order = null;
}

$model->application_layers = PktAppLayer::getAllPacketApplicationLayers();
$model->application_layers = ApplicationLayer::getAllAsObjects();

if ( empty( $model->pktapplayer )) {
foreach ( $model->application_layers as $layer ) {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/Packet/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use \BNETDocs\Libraries\Packet;
use \BNETDocs\Libraries\Product;
use \BNETDocs\Models\Packet\View as PacketViewModel;

use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Controller;
use \CarlBennett\MVC\Libraries\Router;
use \CarlBennett\MVC\Libraries\View as ViewLib;

use \DateTime;
use \DateTimeZone;

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Exceptions/CommentNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CommentNotFoundException extends BNETDocsException {

public function __construct($query, Exception &$prev_ex = null) {
parent::__construct("Comment not found", 21, $prev_ex);
parent::__construct("Comment not found", 18, $prev_ex);
Logger::logMetric("query", $query);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Exceptions/EventNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class EventNotFoundException extends BNETDocsException {

public function __construct($id, Exception &$prev_ex = null) {
parent::__construct('Event not found', 22, $prev_ex);
parent::__construct('Event not found', 19, $prev_ex);
Logger::logMetric('event_id', $id);
}

Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions src/libraries/Exceptions/PacketDirectionInvalidException.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/libraries/Exceptions/PacketTransportLayerNotFoundException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/libraries/Exceptions/ProductNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ProductNotFoundException extends BNETDocsException {

public function __construct($query, Exception &$prev_ex = null) {
parent::__construct("Product not found", 20, $prev_ex);
parent::__construct("Product not found", 17, $prev_ex);
Logger::logMetric("query", $query);
}

Expand Down
9 changes: 3 additions & 6 deletions src/libraries/Exceptions/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ All of the following errors are subclassed from the `BNETDocsException` class.
| 14 | `PacketNotFoundException` | Packet not found |
| 15 | `DocumentNotFoundException` | Document not found |
| 16 | `RecaptchaException` | `$message` |
| 17 | `PacketApplicationLayerNotFoundException` | Packet application layer not found |
| 18 | `PacketTransportLayerNotFoundException` | Packet transport layer not found |
| 19 | `PacketDirectionInvalidException` | Packet direction is invalid |
| 20 | `ProductNotFoundException` | Product not found |
| 21 | `CommentNotFoundException` | Comment not found |
| 22 | `EventNotFoundException` | Event not found |
| 17 | `ProductNotFoundException` | Product not found |
| 18 | `CommentNotFoundException` | Comment not found |
| 19 | `EventNotFoundException` | Event not found |
18 changes: 11 additions & 7 deletions src/libraries/Packet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace BNETDocs\Libraries;

use \BNETDocs\Libraries\Exceptions\PacketDirectionInvalidException;
use \BNETDocs\Libraries\Exceptions\PacketNotFoundException;
use \BNETDocs\Libraries\Exceptions\QueryException;
use \BNETDocs\Libraries\PacketApplicationLayer;
use \BNETDocs\Libraries\PacketTransportLayer;
use \BNETDocs\Libraries\Packet\Application as ApplicationLayer;
use \BNETDocs\Libraries\Packet\Transport as TransportLayer;
use \BNETDocs\Libraries\User;

use \CarlBennett\MVC\Libraries\Common;
Expand All @@ -20,6 +19,7 @@
use \PDO;
use \PDOException;
use \StdClass;
use \UnexpectedValueException;

class Packet implements JsonSerializable {

Expand Down Expand Up @@ -332,7 +332,7 @@ public function getOptionsBitmask() {
}

public function getPacketApplicationLayer() {
return new PacketApplicationLayer( $this->packet_application_layer );
return new ApplicationLayer( $this->packet_application_layer );
}

public function getPacketApplicationLayerId() {
Expand All @@ -349,7 +349,9 @@ public function getPacketDirectionLabel() {
case self::DIRECTION_SERVER_CLIENT: return 'Server to Client';
case self::DIRECTION_PEER_TO_PEER: return 'Peer to Peer';
default:
throw new PacketDirectionInvalidException( $this->packet_direction_id );
throw new UnexpectedValueException(sprintf(
'packet direction: %d is invalid', $this->packet_direction_id
));
}
}

Expand All @@ -359,7 +361,9 @@ public function getPacketDirectionTag() {
case self::DIRECTION_SERVER_CLIENT: return 'S>C';
case self::DIRECTION_PEER_TO_PEER: return 'P2P';
default:
throw new PacketDirectionInvalidException( $this->packet_direction_id );
throw new UnexpectedValueException(sprintf(
'packet direction: %d is invalid', $this->packet_direction_id
));
}
}

Expand Down Expand Up @@ -393,7 +397,7 @@ public function getPacketRemarks( $prepare ) {
}

public function getPacketTransportLayer() {
return new PacketTransportLayer( $this->packet_transport_layer_id );
return new TransportLayer( $this->packet_transport_layer_id );
}

public function getPacketTransportLayerId() {
Expand Down
47 changes: 47 additions & 0 deletions src/libraries/Packet/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace BNETDocs\Libraries\Packet;

use \BNETDocs\Libraries\Packet\Layer;
use \OutOfBoundsException;

class Application extends Layer {

protected static $table = array(
1 => array('Battle.net v1 TCP Messages', 'SID'),
2 => array('Battle.net v1 UDP Messages', 'PKT'),
3 => array('Realm Messages', 'MCP'),
4 => array('D2GS Messages', 'D2GS'),
5 => array('W3GS Messages', 'W3GS'),
6 => array('BotNet Messages', 'PACKET'),
7 => array('BNLS Messages', 'BNLS'),
8 => array('SCGP Messages', 'SCGP'),
9 => array('Battle.net v2 TCP Messages', 'SID2'),
);

protected function assign(int $id) {
if (!isset(self::$table[$id])) {
throw new OutOfBoundsException(sprintf(
'application id: %d not found', $id
));
}

$this->id = $id;
$this->label = self::$table[$id][0];
$this->tag = self::$table[$id][1];
}

public static function getAllAsArray() {
return self::$table;
}

public static function getAllAsObjects() {
$r = array();
$k = array_keys(self::$table);
foreach ($k as $id) {
$r[] = new self($id);
}
return $r;
}

}
31 changes: 31 additions & 0 deletions src/libraries/Packet/Layer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace BNETDocs\Libraries\Packet;

abstract class Layer {

protected $id;
protected $label;
protected $tag;

public function __construct(int $id) {
$this->assign($id);
}

protected abstract function assign(int $id);
public abstract static function getAllAsArray();
public abstract static function getAllAsObjects();

public function getId() {
return $this->id;
}

public function getLabel() {
return $this->label;
}

public function getTag() {
return $this->tag;
}

}
41 changes: 41 additions & 0 deletions src/libraries/Packet/Transport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace BNETDocs\Libraries\Packet;

use \BNETDocs\Libraries\Packet\Layer;
use \OutOfBoundsException;

class Transport extends Layer {

protected static $table = array(
1 => array('Transmission Control Protocol', 'TCP'),
2 => array('User Datagram Protocol', 'UDP'),
3 => array('Internet Control Message Protocol', 'ICMP'),
);

protected function assign(int $id) {
if (!isset(self::$table[$id])) {
throw new OutOfBoundsException(sprintf(
'transport id: %d not found', $id
));
}

$this->id = $id;
$this->label = self::$table[$id][0];
$this->tag = self::$table[$id][1];
}

public static function getAllAsArray() {
return self::$table;
}

public static function getAllAsObjects() {
$r = array();
$k = array_keys(self::$table);
foreach ($k as $id) {
$r[] = new self($id);
}
return $r;
}

}
Loading

0 comments on commit 8570f36

Please sign in to comment.