github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/gp/mux-os.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package gp
     4  
     5  import (
     6  	"../protocol"
     7  )
     8  
     9  type OSMultiplexer struct {
    10  	users          [8]uint16
    11  	appMultiplexer map[uint16]protocol.NetworkTransportMultiplexer // TODO::: map performance penalty!??
    12  }
    13  
    14  // MakeGPNetwork register app to OS GP router and start handle income GP packets.
    15  func (osMux *OSMultiplexer) Init() {
    16  	// send user PublicKey to router and get GP if user granted. otherwise log error.
    17  
    18  	osMux.appMultiplexer = make(map[uint16]protocol.NetworkTransportMultiplexer, 512)
    19  }
    20  
    21  func (osMux *OSMultiplexer) HeaderID() protocol.NetworkLinkNextHeaderID {
    22  	return protocol.NetworkLinkNextHeaderGP
    23  }
    24  
    25  // Receive handle GP packet with any application protocol and response just some basic data!
    26  // Protocol Standard : https://github.com/GeniusesGroup/RFCs/blob/master/Giti-Network.md
    27  func (osMux *OSMultiplexer) Receive(conn protocol.NetworkLinkConnection, packet []byte) {
    28  	// ChaparKhane or OS must always check and penalty other routers or societies
    29  	var err = CheckPacket(packet)
    30  	if err != nil {
    31  		// TODO::: tell router and other society on repeated attack
    32  		return
    33  	}
    34  
    35  	var desAddr Addr = GetDestinationAddr(packet)
    36  	var appMux = osMux.getAppMultiplexer(desAddr.AppID())
    37  	if appMux == nil {
    38  		// TODO::: tell router and other society on repeated attack
    39  		return
    40  	}
    41  	appMux.Receive(conn, packet)
    42  }
    43  
    44  // RegisterAppMultiplexer will register multiplexer only if it is GP multiplexer.
    45  func (osMux *OSMultiplexer) RegisterAppMultiplexer(appMux protocol.NetworkTransportMultiplexer) {
    46  	if appMux.HeaderID() != protocol.NetworkLinkNextHeaderGP {
    47  		return
    48  	}
    49  
    50  	// TODO::: check application have GP access from user!
    51  
    52  	var appID uint16 = protocol.OS.AppManifest().AppID()
    53  	osMux.appMultiplexer[appID] = appMux
    54  }
    55  func (osMux *OSMultiplexer) UnRegisterAppMultiplexer(appMux protocol.NetworkTransportMultiplexer) {
    56  	if appMux.HeaderID() != protocol.NetworkLinkNextHeaderGP {
    57  		return
    58  	}
    59  	var appID uint16 = protocol.OS.AppManifest().AppID()
    60  	osMux.appMultiplexer[appID] = nil
    61  }
    62  func (osMux *OSMultiplexer) getAppMultiplexer(appID uint16) protocol.NetworkTransportMultiplexer {
    63  	return osMux.appMultiplexer[appID]
    64  }
    65  
    66  // Shutdown the listener when the application closes or force to closes by not recovered panic.
    67  func (osMux *OSMultiplexer) Shutdown() {
    68  	// first closing open listener for income packet and refuse all new packet,
    69  	// then closing all idle connections,
    70  	// and then waiting indefinitely for connections to return to idle
    71  	// and then shut down
    72  }