NoobsDeSroobs

Emerald code that does not compile when printing in class.

Apr 13th, 2015
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1.  
  2.  
  3. const tests <- object tests
  4. const home <- locate self
  5. const Server : NopesterServer <- NopesterServer.create
  6. var Clients : Array.of[NopesterClient] <- Array.of[NopesterClient].empty
  7. const Hasher : HashFunction <- BundleAdditiveHash.create
  8. process
  9. stdout.PutString["Starting the program.\n"]
  10. Clients.addupper[NopesterClient.create[Server, Hasher]]
  11. Clients.addupper[NopesterClient.create[Server, Hasher]]
  12. Clients.addupper[NopesterClient.create[Server, Hasher]]
  13. Clients.addupper[NopesterClient.create[Server, Hasher]]
  14.  
  15. var Test : Array.of[Character] <- Array.of[Character].create[3]
  16. test.setelement[0, 'H']
  17. test.setelement[1, 'i']
  18. test.setelement[2, '.']
  19.  
  20. Clients.getelement[0].init["Test"]
  21.  
  22. stdout.PutString[String.literal[Test, 0, Test.upperbound+1] || " From String: ." || "\n"]
  23. stdout.flush
  24.  
  25.  
  26.  
  27. end process
  28. end tests
  29.  
  30. const HashFunction <- typeobject HashFunction
  31. function Hash[Data: String]->[HashID: integer]
  32. end HashFunction
  33.  
  34. const BundleAdditiveHash <- class BundleAdditiveHash
  35. export function Hash[Data : String]->[HashID: integer]
  36. HashID <- 1
  37. var Flip : Integer <- 1
  38. for i : Integer <- 0 while i < Data.upperbound+1 by i <- i + 1
  39. HashID <- -HashID * HashID * Flip + Data.getElement[i].ord * Flip
  40. if i#4 == 0 then
  41. Flip <- flip * -1
  42. end if
  43. end for
  44. end Hash
  45. end BundleAdditiveHash
  46.  
  47.  
  48. const FileDescription <- class File
  49. field KnownPeersForFile: Array.of[NopesterClient] <- Array.of[NopesterClient].empty
  50. field HashID : Integer %//I need to figure out how to do this bit thingy.
  51. field FileSize : Integer
  52. end File
  53.  
  54.  
  55. const FileData <- class FileData
  56. field Data : Array.of[Character] <- Array.of[Character].empty
  57. field FileSize : Integer <- 0
  58. field Downloaded : Array.of[Boolean] <- array.of[Boolean].empty
  59. end FileData
  60.  
  61.  
  62. const NopesterClient <- class nopesterClient[Server : NopesterServer, Hasher : HashFunction]
  63. var FileList : array.of[FileData] <- array.of[Filedata].empty
  64.  
  65.  
  66. export operation Init[FirstString : String]
  67. var newFile : FileData <- FileData.create
  68.  
  69. Newfile.setData[Array.of[character].create[FirstString.length]]
  70.  
  71. for i : Integer <- 0 while i < FirstString.length by i <- i + 1
  72. NewFile$Data.setElement[i, FirstString.getelement[i]]
  73. end for
  74.  
  75. NewFile.setFileSize[FirstString.length]
  76. NewFile.setDownloaded[Array.of[Boolean].create[FirstString.length]]
  77. FileList.addupper[Newfile]
  78.  
  79. stdout.PutString["\n"]
  80.  
  81.  
  82.  
  83. end Init
  84.  
  85. export function getChunk[FileHash:Integer, ChunkID:Integer] -> [Chunk:Character]
  86. var File : FileData <- FileList.getelement[filehash]
  87. if file$Downloaded.getelement[chunkID] then
  88. chunk <- file$Data.getelement[chunkid]
  89. else
  90. chunk <- '0'
  91. end if
  92. end getChunk
  93.  
  94.  
  95. export operation downloadFile[FileHash : Integer] -> []
  96. var NumDownloaded : Integer <- 0
  97. var RequestedFileDesc : FileDescription <- Server.requestFile[Filehash]
  98.  
  99. %//Add check for whether the file already exists on this client. For now assume it is not here.
  100. var CurrFile : FileData <- FileData.create
  101. CurrFile$FileSize <- RequestedFileDesc$FileSize
  102. CurrFile$Data <- Array.of[Character].create[CurrFile$FileSize]
  103. CurrFile$Downloaded <- Array.of[boolean].create[CurrFile$FileSize] %//Does it start as false?
  104.  
  105. loop exit when NumDownloaded >= CurrFile$FileSize
  106. var IndexToGet : integer <- -1
  107.  
  108.  
  109. for i : Integer <- 0 while i < CurrFile$FileSize by i <- i + 1
  110. if !Currfile$Downloaded.getelement[i] then
  111. IndexToGet <- i
  112. exit
  113. end if
  114. end for
  115.  
  116. %//Maybe Add a check for the IndexToGet still being equal to -1.
  117.  
  118. %//Making the assumption that IF a file is being requested, then there is at least one client currently connected that can supply the file.
  119. %//Otherwise this function would spin forever.
  120.  
  121. var ClientIterator : Integer <- 1
  122. var RetrievedChar : Character <- RequestedFileDesc$KnownPeersForFile.getelement[ClientIterator].getChunk[RequestedFileDesc$HashID, IndexToGet]
  123. %//Get that char from someone. If error(0) returns or timeout, ask another in the list. If not, keep asking for the next char until you have them all.
  124. if RetrievedChar == '0' then
  125. ClientIterator <- ClientIterator # RequestedFileDesc$KnownPeersForFile.upperbound
  126. ClientIterator <- ClientIterator + 1
  127. else
  128. CurrFile$Data.setelement[IndexToGet, RetrievedChar]
  129. CurrFile$Downloaded.setelement[IndexToGet, true] %//Set to true to signalize that it has been downloaded.
  130. end if
  131. %//If not error or timeout, set that index in the bool array to true and add the variable to the file array. First time around we must set the size somehow.
  132. NumDownloaded <- NumDownloaded + 1
  133. end loop
  134.  
  135. end downloadFile
  136. end nopesterClient
  137.  
  138.  
  139. const NopesterServer <- class nopesterServer
  140. var KnownPeers: Array.of[NopesterClient]<-Array.of[NopesterClient].empty
  141. var Files: Array.of[FileDescription]<-Array.of[FileDescription].empty
  142.  
  143. export operation addFile[File:FileDescription, Uploader:NopesterClient]
  144.  
  145. %//Maybe we should add some functionality to add all peers given by the client to the list on the server?
  146.  
  147.  
  148. var FileIndex : Integer <- -1
  149. %//Find the hash by looping through all known files.
  150. for i : Integer <- 0 while i < Files.upperbound+1 by i <- i + 1
  151. %//If the input file's hashid is the same as one of the files stored, then save that index.
  152. if Files.getelement[i]$HashID == File$HashID then
  153. FileIndex <- i
  154. exit
  155. end if
  156. end for
  157.  
  158.  
  159. if FileIndex == -1 then
  160. %//Add the new file description.
  161. Files.addupper[File]
  162. Files.getelement[Files.upperbound]$KnownPeersForFile.addupper[Uploader]
  163.  
  164. else
  165. %//Add the client to the list for that file. Add a check for client already ebing in the list.
  166. Files.getelement[FileIndex]$KnownPeersForFile.addupper[Uploader]
  167. end if
  168. end addFile
  169.  
  170. export function requestFile[FileHash:Integer] -> [File:FileDescription]
  171. var FileIndex : Integer <- -1
  172. %//Find the hash by looping through all known files.
  173. for i : Integer <- 0 while i < Files.upperbound+1 by i <- i + 1
  174. %//If the input file's hashid is the same as one of the files stored, then save that index.
  175. if Files.getelement[i]$HashID == File$HashID then
  176. FileIndex <- i
  177. exit
  178. end if
  179. end for
  180.  
  181.  
  182. if FileIndex == -1 then
  183. File <- nil
  184. else
  185. File <- Files.getelement[filehash]
  186. end if
  187. end RequestFile
  188.  
  189. end nopesterServer
Advertisement
Add Comment
Please, Sign In to add comment