Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
snorkels-hs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Linus Heckemann CS2014
snorkels-hs
Commits
e417791c
Commit
e417791c
authored
8 years ago
by
Unai Zalakain
Browse files
Options
Downloads
Patches
Plain Diff
Investigating the use of channels, threads, serialization and sockets
parent
777c1d91
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
snorkels.cabal
+7
-3
7 additions, 3 deletions
snorkels.cabal
src/Snorkels/Broadcaster.hs
+62
-0
62 additions, 0 deletions
src/Snorkels/Broadcaster.hs
src/Snorkels/PlayerTypes/Network.hs
+0
-33
0 additions, 33 deletions
src/Snorkels/PlayerTypes/Network.hs
with
69 additions
and
36 deletions
snorkels.cabal
+
7
−
3
View file @
e417791c
...
...
@@ -60,6 +60,7 @@ executable snorkels
-- Other library packages from which modules are imported.
build-depends: base >=4.8 && <4.9,
bytestring >=0.10.6.0 && <0.10.6.1,
monad-loops >=0.4 && <0.5,
random >=1.1 && <1.2,
containers >=0.5 && <0.6,
...
...
@@ -67,7 +68,8 @@ executable snorkels
bimap >=0.3.2 && <0.3.3,
parsec >=3.1.11 && <3.1.12,
optparse-applicative >=0.13.0 && <0.13.1,
network >=2.6.2.1 && <2.6.2.2
network >=2.6.2.1 && <2.6.2.2,
aeson >=0.8.0.2 && <0.8.0.3
-- Directories containing source files.
hs-source-dirs: src
...
...
@@ -81,8 +83,8 @@ library
exposed-modules: Snorkels.Board,
Snorkels.Game,
Snorkels.Actions,
Snorkels.Broadcaster,
Snorkels.PlayerTypes.Local,
Snorkels.PlayerTypes.Network,
Snorkels.PlayerTypes.RandomAgent
-- Modules included in this library but not exported.
...
...
@@ -93,6 +95,7 @@ library
-- Other library packages from which modules are imported.
build-depends: base >=4.8 && <4.9,
bytestring >=0.10.6.0 && <0.10.6.1,
monad-loops >=0.4 && <0.5,
random >=1.1 && <1.2,
containers >=0.5 && <0.6,
...
...
@@ -100,7 +103,8 @@ library
bimap >=0.3.2 && <0.3.3,
parsec >=3.1.11 && <3.1.12,
optparse-applicative >=0.13.0 && <0.13.1,
network >=2.6.2.1 && <2.6.2.2
network >=2.6.2.1 && <2.6.2.2,
aeson >=0.8.0.2 && <0.8.0.3
-- Directories containing source files.
hs-source-dirs: src
...
...
This diff is collapsed.
Click to expand it.
src/Snorkels/Broadcaster.hs
0 → 100644
+
62
−
0
View file @
e417791c
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
module
Snorkels.Broadcaster
(
Snapshot
,
createChannel
,
broadcast
,
test
)
where
import
Network
import
Control.Concurrent
import
Control.Concurrent.Chan
import
Data.ByteString.Lazy
import
System.IO
import
Data.Aeson
import
Data.Aeson.TH
import
qualified
Data.Bimap
as
Bimap
import
qualified
Data.Map.Strict
as
Map
import
Snorkels.Board
import
Snorkels.Game
type
Snapshot
=
(
Player
,
Either
(
Maybe
Position
)
Player
,
Board
,
Bimap
.
Bimap
Player
Player
)
$
(
deriveJSON
defaultOptions
''Snorkel
)
$
(
deriveJSON
defaultOptions
''Board
)
$
(
deriveJSON
defaultOptions
''Piece
)
$
(
deriveJSON
defaultOptions
''Map
.
Map
)
$
(
deriveJSON
defaultOptions
''Bimap
.
Bimap
)
createChannel
::
IO
(
Chan
Snapshot
)
createChannel
=
newChan
broadcast
::
Chan
Snapshot
->
IO
ThreadId
broadcast
channel
=
forkIO
$
do
sock
<-
listenOn
$
PortNumber
7777
-- Make a duplicate of the channel for this
-- client
channel
<-
dupChan
channel
serve
sock
channel
return
()
serve
::
Socket
->
Chan
Snapshot
->
IO
ThreadId
serve
sock
channel
=
do
(
h
,
_
,
_
)
<-
accept
sock
forkIO
$
body
h
channel
where
body
h
channel
=
do
snaps
<-
getChanContents
channel
hPut
h
$
encode
snaps
hFlush
h
hClose
h
test
=
do
chan
<-
createChannel
let
s
=
(
Green
,
Left
Nothing
,
Board
(
Map
.
fromList
[]
)
(
10
,
10
),
Bimap
.
fromList
[]
)
writeChan
chan
s
broadcast
chan
This diff is collapsed.
Click to expand it.
src/Snorkels/PlayerTypes/Network.hs
deleted
100644 → 0
+
0
−
33
View file @
777c1d91
module
Snorkels.PlayerTypes.Network
(
server
)
where
import
Network
import
Control.Concurrent
import
System.IO
import
Snorkels.Game
-- BUFFER: [(Player, Either (Maybe Position) Switch, Board)]
-- TODO: Switches should be checked too
-- Each program instance maintains a history buffer
-- When it's a network player's turn, connect to its server and asks for change
-- length localBuffer + 1
-- We could generalize this buffer thing so that all player types receive it
server
::
MVar
Game
->
IO
a
server
mVarGame
=
do
sock
<-
listenOn
$
PortNumber
7777
loop
sock
mVarGame
loop
::
Socket
->
MVar
Game
->
IO
a
loop
sock
mVarGame
=
do
(
h
,
_
,
_
)
<-
accept
sock
forkIO
$
body
h
mVarGame
loop
sock
mVarGame
where
body
h
mVarGame
=
do
game
<-
takeMVar
mVarGame
hPutStr
h
$
show
$
currentPlayer
game
hFlush
h
hClose
h
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment