gather all info about peer

This commit is contained in:
DieMyst 2020-11-26 21:47:37 +03:00
parent fa84ead4a4
commit a3bce9f135
13 changed files with 112 additions and 67 deletions

View File

@ -39,7 +39,6 @@
"typescript": "4.1.2", "typescript": "4.1.2",
"webpack": "5.7.0", "webpack": "5.7.0",
"webpack-cli": "4.2.0", "webpack-cli": "4.2.0",
"webpack-dev-server": "3.11.0",
"webpack-nano": "^1.1.0", "webpack-nano": "^1.1.0",
"webpack-plugin-serve": "^1.2.0", "webpack-plugin-serve": "^1.2.0",
"webpack-serve": "3.2.0" "webpack-serve": "3.2.0"

41
src/AirScripts/GetAll.elm Normal file
View File

@ -0,0 +1,41 @@
module AirScripts.GetAll exposing (..)
import Air exposing (Air)
import Air exposing (Air, callBI, fold, next, par, relayEvent, seq, set)
import Json.Encode exposing (list, string)
air : String -> String -> List String -> Air
air peerId relayId peers =
let
clientIdSet =
set "clientId" <| string peerId
relayIdSet =
set "relayId" <| string relayId
peersSet =
set "peers" <| list string peers
airScript =
seq
(callBI "relayId" ( "op", "identity" ) [] Nothing)
(fold "peers" "p" <|
par
(seq
(callBI "p" ( "op", "identify" ) [] (Just "ident"))
(seq
(callBI "p" ( "dist", "get_blueprints" ) [] (Just "blueprints"))
(seq
(callBI "p" ( "dist", "get_modules" ) [] (Just "modules"))
(seq
(callBI "p" ( "srv", "get_interfaces" ) [] (Just "interfaces"))
(relayEvent "all_info" [ "p", "ident", "interfaces","blueprints", "modules" ])
)
)
)
)
(next "p")
)
in
clientIdSet <| relayIdSet <| peersSet <| airScript

7
src/Blueprints/Air.elm Normal file
View File

@ -0,0 +1,7 @@
module Blueprints.Air exposing (..)
import Air exposing (Air)
import AirScripts.CallPeers
air : String -> String -> List String -> Air
air peerId relayId peers =
AirScripts.CallPeers.air peerId relayId ("blueprints_discovered", "dist", "get_blueprints") peers

7
src/Blueprints/Model.elm Normal file
View File

@ -0,0 +1,7 @@
module Blueprints.Model exposing (..)
type alias Blueprint =
{ dependencies: List String
, id: String
, name: String
}

View File

@ -16,8 +16,10 @@ limitations under the License.
-} -}
import Blueprints.Model exposing (Blueprint)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Dict exposing (Dict) import Dict exposing (Dict)
import Nodes.Model exposing (Identify, emptyIdentify)
import Services.Model exposing (Service) import Services.Model exposing (Service)
import Url import Url
@ -28,14 +30,16 @@ type Route
type alias PeerData = type alias PeerData =
{ services : List Service { identify : Identify
, services : List Service
, modules : List String , modules : List String
, blueprints : List Blueprint
} }
emptyPeerData : PeerData emptyPeerData : PeerData
emptyPeerData = emptyPeerData =
{ services = [], modules = [] } { identify = emptyIdentify, services = [], modules = [], blueprints = [] }
type alias Model = type alias Model =

7
src/Nodes/Model.elm Normal file
View File

@ -0,0 +1,7 @@
module Nodes.Model exposing (..)
type alias Identify =
{ external_addresses: List String }
emptyIdentify : Identify
emptyIdentify = { external_addresses = [] }

View File

@ -1,8 +1,10 @@
port module Port exposing (..) port module Port exposing (..)
import Air exposing (Air(..)) import Air exposing (Air(..))
import Blueprints.Model exposing (Blueprint)
import Dict exposing (Dict) import Dict exposing (Dict)
import Json.Encode exposing (Value) import Json.Encode exposing (Value)
import Nodes.Model exposing (Identify)
import Services.Model exposing (Service) import Services.Model exposing (Service)
@ -11,7 +13,7 @@ type alias SendParticle =
type alias ReceiveEvent = type alias ReceiveEvent =
{ name : String, peer : String, peers : Maybe (List String), services : Maybe (List Service), modules : Maybe (List String) } { name : String, peer : String, peers : Maybe (List String), identify : Maybe Identify, services : Maybe (List Service), modules : Maybe (List String), blueprints : Maybe (List Blueprint)}
port sendParticle : SendParticle -> Cmd msg port sendParticle : SendParticle -> Cmd msg

View File

@ -28,6 +28,8 @@ routeView model route =
case route of case route of
Page page -> Page page ->
case page of case page of
"" ->
HubPage.view {}
"hub" -> "hub" ->
HubPage.view {} HubPage.view {}

View File

@ -16,6 +16,8 @@ limitations under the License.
-} -}
import AirScripts.GetAll
import Blueprints.Model exposing (Blueprint)
import Browser import Browser
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Dict import Dict
@ -26,9 +28,11 @@ import Model exposing (Model, emptyPeerData)
import Modules.Air import Modules.Air
import Msg exposing (..) import Msg exposing (..)
import Nodes.Air import Nodes.Air
import Nodes.Model exposing (Identify)
import Port exposing (sendAir) import Port exposing (sendAir)
import Route import Route
import Services.Air import Services.Air
import Services.Model exposing (Service)
import Url import Url
@ -103,7 +107,7 @@ update msg model =
Browser.External href -> Browser.External href ->
( model, Nav.load href ) ( model, Nav.load href )
AquamarineEvent { name, peer, peers, services, modules } -> AquamarineEvent { name, peer, peers, identify, services, modules, blueprints } ->
case name of case name of
"peers_discovered" -> "peers_discovered" ->
let let
@ -118,21 +122,11 @@ update msg model =
in in
( { model | discoveredPeers = updatedDict }, Cmd.none ) ( { model | discoveredPeers = updatedDict }, Cmd.none )
"services_discovered" -> "all_info" ->
let let
newServices = updated = Maybe.map4 (updateModel model peer) identify services modules blueprints
Maybe.withDefault [] services
empty =
emptyPeerData
up =
\old -> Just (Maybe.withDefault { empty | services = newServices } (Maybe.map (\o -> { o | services = newServices }) old))
updatedDict =
Dict.update peer up model.discoveredPeers
in in
( { model | discoveredPeers = updatedDict }, Cmd.none ) ( withDefault model updated, Cmd.none )
"modules_discovered" -> "modules_discovered" ->
let let
@ -159,17 +153,9 @@ update msg model =
Click command -> Click command ->
case command of case command of
"get_services" -> "get_all" ->
( model ( model
, sendAir (Services.Air.air model.peerId model.relayId (Dict.keys model.discoveredPeers)) , sendAir (AirScripts.GetAll.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
)
"get_modules" ->
( model
, sendAir (Modules.Air.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
)
"get_identify" ->
( model
, sendAir (Nodes.Air.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
) )
_ -> _ ->
(model, Cmd.none) (model, Cmd.none)
@ -177,3 +163,13 @@ update msg model =
RelayChanged relayId -> RelayChanged relayId ->
( { model | relayId = relayId }, Cmd.none ) ( { model | relayId = relayId }, Cmd.none )
updateModel : Model -> String -> Identify -> List Service -> List String -> List Blueprint -> Model
updateModel model peer identify services modules blueprints =
let
data = Maybe.withDefault emptyPeerData (Dict.get peer model.discoveredPeers)
newData = { data | identify = identify, services = services, modules = modules, blueprints = blueprints }
updated = Dict.insert peer newData model.discoveredPeers
in
{ model | discoveredPeers = updated }

View File

@ -16,6 +16,11 @@ limitations under the License.
-} -}
import Maybe exposing (map2)
combine : List (Maybe a) -> Maybe (List a)
combine =
List.foldr (map2 (::)) (Just [])
isEmpty : Maybe a -> Bool isEmpty : Maybe a -> Bool
isEmpty maybe = isEmpty maybe =

View File

@ -18,7 +18,6 @@ limitations under the License.
import Browser exposing (Document, UrlRequest(..)) import Browser exposing (Document, UrlRequest(..))
import Html exposing (Html, div, header, text) import Html exposing (Html, div, header, text)
import Html.Attributes exposing (class, classList)
import Html.Events exposing (onClick) import Html.Events exposing (onClick)
import Model exposing (Model, Route(..)) import Model exposing (Model, Route(..))
import Msg exposing (..) import Msg exposing (..)
@ -38,21 +37,10 @@ title _ =
body : Model -> Html Msg body : Model -> Html Msg
body model = body model =
let
a =
1
url =
model.url
newUrl =
{ url | path = "/hub" }
in
layout <| layout <|
List.concat List.concat
[ [ header [ classes "w-100 bt bb b--black-10" ] [ routeView model (Page "hub") ] ] [ [ header [ classes "w-100 bt bb b--black-10" ] [ routeView model model.page ] ]
++ [ header [ classes "w-100 bt bb b--black-10" ] [ routeView model (Page "module") ] ] ++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_all") ] [ text "GET SERVICES" ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_services") ] [ text "GET SERVICES" ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_modules") ] [ text "GET MODULES" ] ] ++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_modules") ] [ text "GET MODULES" ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_identify") ] [ text "GET IDENTIFY" ] ] ++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_identify") ] [ text "GET IDENTIFY" ] ]
] ]

View File

@ -32,20 +32,14 @@ function genFlags(peerId: string): any {
} }
} }
function event(name: string, peer: string, peers?: string[], services?: any[], modules?: string[]) { function event(name: string, peer: string, peers?: string[], identify?: string[], services?: any[], blueprints?: string[], modules?: string[]) {
if (!peers) { if (!peers) { peers = null }
peers = null if (!services) { services = null }
} if (!modules) { modules = null }
if (!identify) { identify = null }
if (!blueprints) { blueprints = null }
if (!services) { return {name, peer, peers, identify, services, modules, blueprints}
services = null
}
if (!modules) {
modules = null
}
return {name, peer, peers, services, modules}
} }
(async () => { (async () => {
@ -70,10 +64,8 @@ function event(name: string, peer: string, peers?: string[], services?: any[], m
try { try {
if (fnName === "peers_discovered") { if (fnName === "peers_discovered") {
app.ports.eventReceiver.send(event(fnName, args[0], args[1])) app.ports.eventReceiver.send(event(fnName, args[0], args[1]))
} else if (fnName === "services_discovered") { } else if (fnName === "all_info") {
app.ports.eventReceiver.send(event(fnName, args[0], undefined, args[1])) app.ports.eventReceiver.send(event(fnName, args[0], undefined, args[1], args[2], args[3], args[4]))
} else if (fnName === "modules_discovered") {
app.ports.eventReceiver.send(event(fnName, args[0], undefined, undefined, args[1]))
} else { } else {
console.error("UNHANDLED") console.error("UNHANDLED")
} }

View File

@ -13,13 +13,7 @@ module.exports = {
devServer: { devServer: {
contentBase: './bundle', contentBase: './bundle',
hot: false, hot: false,
inline: false, inline: false
historyApiFallback: {
rewrites: [
{from: /^\/$/, to: '/index.html'},
{from: /./, to: '/index.html'}
]
}
}, },
devtool: "eval-source-map", devtool: "eval-source-map",
module: { module: {
@ -72,7 +66,8 @@ module.exports = {
}), }),
new Serve({ new Serve({
historyFallback: true, historyFallback: true,
port: 55553 port: 55553,
host: 'localhost',
}) })
] ]
}; };