Last active
December 29, 2015 00:48
-
-
Save SPY/7588255 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE BangPatterns, DeriveDataTypeable, FlexibleInstances, MultiParamTypeClasses #-} | |
module Proto.King.Hello (Hello(..)) where | |
import Prelude ((+), (/)) | |
import qualified Prelude as Prelude' | |
import qualified Data.Typeable as Prelude' | |
import qualified Data.Data as Prelude' | |
import qualified Text.ProtocolBuffers.Header as P' | |
import qualified Proto.King.NodeDescription as King (NodeDescription) | |
data Hello = Hello{hNodeDesc :: !King.NodeDescription} | |
deriving (Prelude'.Show, Prelude'.Eq, Prelude'.Ord, Prelude'.Typeable, Prelude'.Data) | |
instance P'.Mergeable Hello where | |
mergeAppend (Hello x'1) (Hello y'1) = Hello (P'.mergeAppend x'1 y'1) | |
instance P'.Default Hello where | |
defaultValue = Hello P'.defaultValue | |
instance P'.Wire Hello where | |
wireSize ft' self'@(Hello x'1) | |
= case ft' of | |
10 -> calc'Size | |
11 -> P'.prependMessageSize calc'Size | |
_ -> P'.wireSizeErr ft' self' | |
where | |
calc'Size = (P'.wireSizeReq 1 11 x'1) | |
wirePut ft' self'@(Hello x'1) | |
= case ft' of | |
10 -> put'Fields | |
11 -> do | |
P'.putSize (P'.wireSize 10 self') | |
put'Fields | |
_ -> P'.wirePutErr ft' self' | |
where | |
put'Fields | |
= do | |
P'.wirePutReq 10 11 x'1 | |
wireGet ft' | |
= case ft' of | |
10 -> P'.getBareMessageWith update'Self | |
11 -> P'.getMessageWith update'Self | |
_ -> P'.wireGetErr ft' | |
where | |
update'Self wire'Tag old'Self | |
= case wire'Tag of | |
10 -> Prelude'.fmap (\ !new'Field -> old'Self{hNodeDesc = P'.mergeAppend (hNodeDesc old'Self) (new'Field)}) | |
(P'.wireGet 11) | |
_ -> let (field'Number, wire'Type) = P'.splitWireTag wire'Tag in P'.unknown field'Number wire'Type old'Self | |
instance P'.MessageAPI msg' (msg' -> Hello) Hello where | |
getVal m' f' = f' m' | |
instance P'.GPB Hello | |
instance P'.ReflectDescriptor Hello where | |
getMessageInfo _ = P'.GetMessageInfo (P'.fromDistinctAscList [10]) (P'.fromDistinctAscList [10]) | |
reflectDescriptorInfo _ | |
= Prelude'.read | |
"DescriptorInfo {descName = ProtoName {protobufName = FIName \".King.Hello\", haskellPrefix = [MName \"Proto\"], parentModule = [MName \"King\"], baseName = MName \"Hello\"}, descFilePath = [\"Proto\",\"King\",\"Hello.hs\"], isGroup = False, fields = fromList [FieldInfo {fieldName = ProtoFName {protobufName' = FIName \".King.Hello.hNodeDesc\", haskellPrefix' = [MName \"Proto\"], parentModule' = [MName \"King\",MName \"Hello\"], baseName' = FName \"hNodeDesc\"}, fieldNumber = FieldId {getFieldId = 1}, wireTag = WireTag {getWireTag = 10}, packedTag = Nothing, wireTagLength = 1, isPacked = False, isRequired = True, canRepeat = False, mightPack = False, typeCode = FieldType {getFieldType = 11}, typeName = Just (ProtoName {protobufName = FIName \".King.NodeDescription\", haskellPrefix = [MName \"Proto\"], parentModule = [MName \"King\"], baseName = MName \"NodeDescription\"}), hsRawDefault = Nothing, hsDefault = Nothing}], keys = fromList [], extRanges = [], knownKeys = fromList [], storeUnknown = False, lazyFields = False}" |
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
import Messages | |
main :: IO () | |
main = do | |
input <- newChan | |
receiver input | |
forver $ do | |
msg <- readChan input | |
putStrLn $ show msg |
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
class Message a where | |
pack :: a -> ByteString | |
unpack :: ByteString -> Maybe a | |
data HelloMsg = HelloMsg deriving Show | |
instance Message HelloMsg -- ... using protobuf | |
data PingMsg = PingMsg deriving Show | |
instance Message PingMsg -- ... using protobuf | |
data PongMsg = PongMsg deriving Show | |
instance Message PongMsg -- ... using protobuf | |
-- and more 50 another... | |
data MsgType = Hello | Ping | Pong -- | and more 50 another ... | |
data CommonMsg a = CommonMsg MsgType a deriving Show | |
instance (Message a) => CommonMsg a where | |
pack (CommonMsg type msg) = concat [packType type, pack msg] | |
unpack bin = let (type, rest) = unpackType bin in CommonMsg type <$> unpack rest | |
receiver :: Chan (CommonMsg a) -> IO () | |
receiver out = void $ forkIO $ forever $ do | |
msg <- unpack <$> getCommonMsgFromNet | |
writeChan out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment