-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a77961c
commit c32eca0
Showing
11 changed files
with
148 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package api_gateway | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/it-chain/engine/common/event" | ||
) | ||
|
||
var ErrConnectionExists = errors.New("Connection already exists") | ||
|
||
type ConnectionQueryApi struct { | ||
mux *sync.Mutex | ||
connectionRepository ConnectionRepository | ||
} | ||
|
||
func NewConnectionQueryApi(connRepo ConnectionRepository) *ConnectionQueryApi { | ||
return &ConnectionQueryApi{ | ||
mux: &sync.Mutex{}, | ||
connectionRepository: connRepo, | ||
} | ||
} | ||
|
||
func (q ConnectionQueryApi) GetAllConnectionList() ([]Connection, error) { | ||
return q.connectionRepository.FindAll() | ||
} | ||
|
||
func (q ConnectionQueryApi) GetConnectionByID(connID string) (Connection, error) { | ||
return q.connectionRepository.FindByID(connID) | ||
} | ||
|
||
type ConnectionRepository struct { | ||
mux *sync.RWMutex | ||
ConnectionTable map[string]Connection | ||
} | ||
|
||
func NewConnectionRepository() *ConnectionRepository { | ||
return &ConnectionRepository{ | ||
mux: &sync.RWMutex{}, | ||
ConnectionTable: make(map[string]Connection), | ||
} | ||
} | ||
|
||
func (cr *ConnectionRepository) Save(conn Connection) error { | ||
cr.mux.Lock() | ||
defer cr.mux.Unlock() | ||
|
||
_, exist := cr.ConnectionTable[conn.ConnectionID] | ||
if exist { | ||
return ErrConnectionExists | ||
} | ||
|
||
cr.ConnectionTable[conn.ConnectionID] = conn | ||
|
||
return nil | ||
} | ||
|
||
func (cr *ConnectionRepository) Remove(connID string) error { | ||
cr.mux.Lock() | ||
defer cr.mux.Unlock() | ||
|
||
delete(cr.ConnectionTable, connID) | ||
|
||
return nil | ||
} | ||
|
||
func (cr *ConnectionRepository) FindAll() ([]Connection, error) { | ||
cr.mux.Lock() | ||
defer cr.mux.Unlock() | ||
|
||
connectionList := []Connection{} | ||
|
||
for _, conn := range cr.ConnectionTable { | ||
connectionList = append(connectionList, conn) | ||
} | ||
|
||
return connectionList, nil | ||
} | ||
|
||
func (cr *ConnectionRepository) FindByID(connID string) (Connection, error) { | ||
cr.mux.Lock() | ||
defer cr.mux.Unlock() | ||
|
||
for _, conn := range cr.ConnectionTable { | ||
if connID == conn.ConnectionID { | ||
return conn, nil | ||
} | ||
} | ||
|
||
return Connection{}, nil | ||
} | ||
|
||
type ConnectionEventListener struct { | ||
connectionRepository ConnectionRepository | ||
} | ||
|
||
func NewConnectionEventListener(connRepo ConnectionRepository) ConnectionEventListener { | ||
return ConnectionEventListener{ | ||
connectionRepository: connRepo, | ||
} | ||
} | ||
|
||
func (cel *ConnectionEventListener) HandleConnectionCreatedEvent(event event.ConnectionCreated) error { | ||
connection := Connection{ | ||
ConnectionID: event.ConnectionID, | ||
Address: event.Address, | ||
} | ||
|
||
err := cel.connectionRepository.Save(connection) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cel *ConnectionEventListener) HandleConnectionClosedEvent(event event.ConnectionClosed) error { | ||
cel.connectionRepository.Remove(event.ConnectionID) | ||
return nil | ||
} | ||
|
||
type Connection struct { | ||
ConnectionID string | ||
Address string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,6 @@ | |
package grpc_gateway | ||
|
||
type Connection struct { | ||
ConnectionId string | ||
ConnectionID string | ||
Address string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters