Created frames table with migration

This commit is contained in:
Deon George 2018-12-03 23:59:22 +11:00
parent 4504818e25
commit 3651a6508a
11 changed files with 1049 additions and 732 deletions

View File

@ -56,7 +56,7 @@ class Frame
if (! $this->hasFlag('ip')) {
// Set the page header: CUG/Site Name | Page # | Cost
$this->output .= $this->render_header($this->header).
$this->render_page($this->frame->frame_id,$this->frame->subframe_id).
$this->render_page($this->frame->frame,$this->frame->index).
$this->render_cost($this->frame->cost);
}
@ -93,14 +93,14 @@ class Frame
for ($x=0;$x<$this->frame_width;$x++)
{
$posn = $y*40+$x;
$byte = ord(isset($this->frame->frame_content{$posn}) ? $this->frame->frame_content{$posn} : ' ')%128;
$byte = ord(isset($this->frame->content{$posn}) ? $this->frame->content{$posn} : ' ')%128;
// dump(sprintf('Y: %s,X: %s, POSN: %s, BYTE: %s',$y,$x,$posn,$byte));
// Check for start-of-field
if ($byte == ord(ESC)) { // Esc designates start of field (Esc-K is end of edit)
$infield = TRUE;
$fieldlength = 0;
$fieldtype = ord(substr($this->frame->frame_content,$posn+1,1))%128;
$fieldtype = ord(substr($this->frame->content,$posn+1,1))%128;
$byte = ord(' '); // Replace ESC with space.
} else {
@ -132,7 +132,7 @@ class Frame
// Replace field with Date
if ($field == '%date') {
if ($fieldlength == 1)
$datetime = strtoupper(date('D d M Y H:i:s'));
$datetime = date('D d M H:ia');
if ($fieldlength <= strlen($datetime))
$byte = ord($datetime{$fieldlength-1});
@ -168,7 +168,7 @@ class Frame
}
// truncate end of lines
if (isset($pageflags['tru']) && substr($this->frame->frame_content,$y*40+$x,40-$x) === str_repeat(' ',40-$x)) {
if (isset($pageflags['tru']) && substr($this->frame->content,$posn,40-$x) === str_repeat(' ',40-$x)) {
$this->output .= CR . LF;
break;
}
@ -183,7 +183,7 @@ class Frame
*/
public function framenum()
{
return $this->frame->frame_id.$this->frame->subframe_id;
return $this->frame->frame.$this->frame->index;
}
/**
@ -253,6 +253,17 @@ class Frame
return sprintf(WHITE.'% '.$this->pagenum_length.'.0f%s',$num,$frame);
}
/**
* Get the route for the key press
*
* @param string $read
*/
public function route(string $read)
{
// @todo
return FALSE;
}
/**
* Calculate the length of text
*
@ -270,21 +281,21 @@ class Frame
// Simulate a DB load
$o = new \App\Models\Frame;
$o->frame_content = '';
$o->content = '';
$o->flags = ['ip'];
$o->frametype = 'a';
$o->frame_id = 999;
$o->subframe_id = 'a';
$o->type = 'a';
$o->frame = 999;
$o->index = 'a';
// Header
$o->frame_content .= substr(R_RED.'T'.R_BLUE.'E'.R_GREEN.'S'.R_YELLOW.'T-12345678901234567890',0,20).
$o->content .= substr(R_RED.'T'.R_BLUE.'E'.R_GREEN.'S'.R_YELLOW.'T-12345678901234567890',0,20).
R_WHITE.'999999999a'.R_RED.sprintf('%07.0f',999).'u';
$o->frame_content .= str_repeat('+-',18).' '.R_RED.'01';
$o->frame_content .= 'Date: '.ESC.str_repeat('d',25).str_repeat('+-',4);
$o->frame_content .= 'Name: '.ESC.str_repeat('u',5).str_repeat('+-',14);
$o->frame_content .= 'Address: '.ESC.str_repeat('a',20).''.str_repeat('+-',5);
$o->frame_content .= ' : '.ESC.str_repeat('a',20).''.str_repeat('+-',5);
$o->content .= str_repeat('+-',18).' '.R_RED.'01';
$o->content .= 'Date: '.ESC.str_repeat('d',25).str_repeat('+-',4);
$o->content .= 'Name: '.ESC.str_repeat('u',5).str_repeat('+-',14);
$o->content .= 'Address: '.ESC.str_repeat('a',20).''.str_repeat('+-',5);
$o->content .= ' : '.ESC.str_repeat('a',20).''.str_repeat('+-',5);
return $o;
}

View File

@ -0,0 +1,61 @@
<?php
namespace App\Console\Commands;
use App\Models\Frame;
use Illuminate\Console\Command;
class FrameDelete extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'frame:delete {frame} {index}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete frames from the database.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
if (! is_numeric($this->argument('frame')))
throw new \Exception('Frame is not numeric: '.$this->argument('frame'));
if (strlen($this->argument('index')) != 1 OR ! preg_match('/^[a-z]$/',$this->argument('index')))
throw new \Exception('Subframe failed validation');
try {
$o = Frame::where('frame',$this->argument('frame'))
->where('index',$this->argument('index'))
->firstOrFail();
} catch (ModelNotFoundException $e) {
$this->error('Page not found to delete: '.$this->argument('frame').$this->argument('index'));
die(1);
}
$o->delete();
$this->info('Page deleted: '.$this->argument('frame').$this->argument('index'));
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace App\Console\Commands;
use App\Models\Frame;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class FrameImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'frame:import {frame} {index} {file} '.
'{--cost=0 : Frame Cost }'.
'{--mode=1 : Frame Emulation Mode }'.
'{--replace : Replace existing frame}'.
'{--type=i : Frame Type}'.
'{--trim : Trim off header (first 40 chars)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import frames into the database. The frames should be in binary format.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
if (! is_numeric($this->argument('frame')))
throw new \Exception('Frame is not numeric: '.$this->argument('frame'));
if (strlen($this->argument('index')) != 1 OR ! preg_match('/^[a-z]$/',$this->argument('index')))
throw new \Exception('Subframe failed validation');
if (! file_exists($this->argument('file')))
throw new \Exception('File not found: '.$this->argument('file'));
$o = new Frame;
if ($this->option('replace')) {
try {
$o = $o->where('frame',$this->argument('frame'))
->where('index',$this->argument('index'))
->firstOrFail();
} catch (ModelNotFoundException $e) {
$this->error('Page not found to replace: '.$this->argument('frame').$this->argument('index'));
die(1);
}
} else {
$o->frame = $this->argument('frame');
$o->index = $this->argument('index');
}
$o->content = ($this->option('trim'))
? substr(file_get_contents($this->argument('file')),40)
: file_get_contents($this->argument('file'));
$o->cost = $this->option('cost');
$o->mode_id = $this->option('mode');
$o->type = $this->option('type');
$o->save();
}
}

View File

@ -79,6 +79,8 @@ class Server extends Command
/**
* Connection handler
*
* @todo: Pressing * three times gets "Shouldnt get here".
*/
function onConnect(SocketClient $client) {
Log::info('Connection from: ',['client'=>$client->getAddress()]);
@ -97,8 +99,7 @@ class Server extends Command
return;
// We are now the child.
// @todo Need to intercept any crashes and close the TCP port.
try {
$session_init = $session_option = FALSE;
$session_note = ''; // TCP Session Notice
$session_term = ''; // TCP Terminal Type
@ -110,12 +111,11 @@ class Server extends Command
$client->send(TCP_IAC . TCP_DO . TCP_OPT_TERMTYPE . TCP_IAC . TCP_SB . TCP_OPT_TERMTYPE . TCP_OPT_ECHO . TCP_IAC . TCP_SE); // Request Term Type
$client->send(CLS);
$read = '';
// @todo Deprecate and have an exception handler handle this.
// like throw new FatalClientException(CLS.UP.ERR_DATABASE,500,NULL,$client);
// @todo Get the login page, and if it is not available, throw the ERR_DATEBASE error.
// @todo Get the login/start page, and if it is not available, throw the ERR_DATEBASE error.
$db = new \vvdb();
// Connect to database. Returns error message if unable to connect.
@ -141,7 +141,7 @@ class Server extends Command
$history = array(); // backup history
$cmd = ''; // current *command being typed in
$cmd = ''; // Current *command being typed in
$mode = false; // input mode.
$prevmode = false; // previous mode
$timewarp = false;
@ -153,7 +153,7 @@ class Server extends Command
$page = $service['start_page'];
$subpage = 'a';
} else {
$page = '999'; // next page
$page = '98'; // next page
$subpage = 'a';
}
$curpage = ''; // current page
@ -192,8 +192,7 @@ class Server extends Command
// Client initiation input
// TELNET http://pcmicro.com/netfoss/telnet.html
if ($read == TCP_IAC OR $session_init OR $session_option)
{
if ($read == TCP_IAC OR $session_init OR $session_option) {
Log::debug(sprintf('Session Char (%s)', ord($read)), ['init' => $session_init, 'option' => $session_option]);
switch ($read) {
@ -363,6 +362,7 @@ class Server extends Command
// Other Frame Types - Shouldnt get here.
default:
$client->close();
throw new \Exception('Shouldnt get here', 500);
} // switch frame types
@ -492,7 +492,7 @@ class Server extends Command
// Not doing anything in particular.
case FALSE:
$cmd = '';
Log::debug('Idle',['pid'=>'TBA']);
Log::debug('Idle', ['pid' => $childpid]);
switch ($read) {
case HASH:
@ -514,17 +514,15 @@ class Server extends Command
case '7':
case '8':
case '9':
if (isset($pagedata['route' . $read]) && $pagedata['route' . $read] != '*') {
if ($frame = $fo->route($read)) {
$action = ACTION_GOTO;
$page = $pagedata['route' . $read];
$subpage = 'a';
break;
} else {
sendBaseline($client, $blp, ERR_ROUTE);
$mode = $action = false;
$mode = $action = FALSE;
}
break;
break;
}
break;
@ -624,7 +622,6 @@ class Server extends Command
break;
case ACTION_SUBMITRF:
$action = false;
sendBaseline($client, $bpl, MSG_SENDORNOT);
@ -632,8 +629,6 @@ class Server extends Command
break;
case ACTION_BACKUP:
// do we have anywhere to go?
if (count($history) > 1) { // because current page should always be in there.
@ -735,7 +730,6 @@ class Server extends Command
break;
case 'a': // active frame. Prestel uses this for Response Framea.
/*
if ($timewarp && 1 < count(
$alts = $db->getAlternateFrames($service['service_id'],$varient['varient_id'],$curpage,$cursub)
@ -805,8 +799,6 @@ class Server extends Command
break;
case ACTION_INFO: // special emulator command
$mode = false;
$cmd = '';
@ -860,10 +852,16 @@ class Server extends Command
if ($read === null || socket_last_error()) {
printf("[%s] Disconnected\n", $client->getaddress());
return false;
}
}
// Something bad happened. We'll log it and then disconnect.
} catch (\Exception $e) {
Log::emergency($e->getMessage());
}
$client->close();
printf( "[%s] Disconnected\n", $client->getaddress() );
}
@ -873,6 +871,7 @@ class Server extends Command
return strlen($text) - substr_count($text, ESC);
}
// @todo Not clearing to end of line.
function sendBaseline($client, &$blp, $text, $reposition = false){
$client->send(HOME . UP . $text .
( $blp > strlenv($text) ? str_repeat(' ',$blp-strlenv($text)) .

View File

@ -13,25 +13,14 @@ class Frame extends Model
* @param string $frame
* @return mixed
*/
public function fetch(int $page,string $frame): \App\Classes\Frame
public function fetch(int $frame,string $index): \App\Classes\Frame
{
if ($page == '999' and $frame == 'a')
if ($frame == '999' and $index == 'a')
{
return new \App\Classes\Frame(\App\Classes\Frame::testFrame());
}
return new \App\Classes\Frame($this->where('frame_id',$page)->where('subframe_id',$frame)->firstOrFail());
}
/**
* Return the frame cost
*
* @return int
*/
public function getCostAttribute()
{
// @todo NOT in DB
return rand(0,999);
return new \App\Classes\Frame($this->where('frame',$frame)->where('index',$index)->firstOrFail());
}
public function hasFlag(string $flag)
@ -45,7 +34,6 @@ class Frame extends Model
*/
public function type()
{
// @todo in DB
return isset($this->frametype) ? $this->frametype : 'i';
return $this->type ?: 'i';
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace App\Traits;
use \Illuminate\Database\Eloquent\Builder;
/**
* Trait HasCompositePrimaryKey
* Enables primary keys to be an array.
*
* @package App\Traits
*/
trait HasCompositePrimaryKey {
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
$keys = $this->getKeyName();
if (! is_array($keys)){
return parent::setKeysForSaveQuery($query);
}
foreach ($keys as $keyName){
$query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
}
return $query;
}
/**
* Get the primary key value for a save query.
*
* @param mixed $keyName
* @return mixed
*/
protected function getKeyForSaveQuery($keyName = null)
{
if (is_null($keyName)){
$keyName = $this->getKeyName();
}
if (isset($this->original[$keyName])) {
return $this->original[$keyName];
}
return $this->getAttribute($keyName);
}
}

View File

@ -40,7 +40,7 @@
"App\\": "app/"
},
"files": [
"bootstrap/constants.php"
"config/constants.php"
],
"classmap": [
"database/seeds",

View File

@ -0,0 +1,79 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class InitDatabase extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->down();
Schema::create('modes', function (Blueprint $table) {
$table->timestamps();
$table->integer('id')->autoIncrement();
$table->string('name',16);
$table->string('note',255);
$table->unique(['name']);
});
Schema::create('cugs', function (Blueprint $table) {
$table->timestamps();
$table->integer('id');
$table->string('name',16);
$table->string('note',255);
$table->integer('parent_id')->nullable();
$table->primary('id');
$table->foreign('parent_id')->references('id')->on('cugs');
$table->unique(['name']);
});
Schema::create('frames', function (Blueprint $table) {
$table->timestamps();
$table->integer('id')->autoIncrement();
$table->integer('frame');
$table->char('index',1);
$table->integer('mode_id');
$table->char('type',2);
$table->smallInteger('cost')->default(0);
$table->integer('cug_id')->default(0);
$table->boolean('public')->default(FALSE);
$table->binary('content');
$table->string('note',255)->nullable();
//$table->unique(['frame','index','mode_id']); // Not needed since we have timewarp
$table->foreign('mode_id')->references('id')->on('modes');
$table->foreign('cug_id')->references('id')->on('cugs');
});
Schema::create('routes', function (Blueprint $table) {
$table->integer('id')->autoIncrement();
$table->char('key',1);
$table->integer('route');
$table->foreign('route')->references('id')->on('frames');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('routes');
Schema::dropIfExists('frames');
Schema::dropIfExists('cugs');
Schema::dropIfExists('modes');
}
}

View File

@ -11,6 +11,7 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(SeedMode::class);
$this->call(SeedCUG::class);
}
}

View File

@ -0,0 +1,21 @@
<?php
use Illuminate\Database\Seeder;
class SeedCUG extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('cugs')->insert([
'created_at' => now(),
'id' => 0,
'name' => 'Public Frames',
'note' => 'All frames belong to this CUG if not any other.',
]);
}
}

View File

@ -0,0 +1,20 @@
<?php
use Illuminate\Database\Seeder;
class SeedMode extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('modes')->insert([
'created_at' => now(),
'name' => 'ViewData',
'note' => 'Original ViewData/VideoTex 40x25 char mode.',
]);
}
}