Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const tests <- object tests
- const home <- locate self
- const Server : NopesterServer <- NopesterServer.create
- var Clients : Array.of[NopesterClient] <- Array.of[NopesterClient].empty
- const Hasher : HashFunction <- BundleAdditiveHash.create
- process
- stdout.PutString["Starting the program.\n"]
- Clients.addupper[NopesterClient.create[Server, Hasher]]
- Clients.addupper[NopesterClient.create[Server, Hasher]]
- Clients.addupper[NopesterClient.create[Server, Hasher]]
- Clients.addupper[NopesterClient.create[Server, Hasher]]
- var Test : Array.of[Character] <- Array.of[Character].create[3]
- test.setelement[0, 'H']
- test.setelement[1, 'i']
- test.setelement[2, '.']
- Clients.getelement[0].init["Test"]
- stdout.PutString[String.literal[Test, 0, Test.upperbound+1] || " From String: ." || "\n"]
- stdout.flush
- end process
- end tests
- const HashFunction <- typeobject HashFunction
- function Hash[Data: String]->[HashID: integer]
- end HashFunction
- const BundleAdditiveHash <- class BundleAdditiveHash
- export function Hash[Data : String]->[HashID: integer]
- HashID <- 1
- var Flip : Integer <- 1
- for i : Integer <- 0 while i < Data.upperbound+1 by i <- i + 1
- HashID <- -HashID * HashID * Flip + Data.getElement[i].ord * Flip
- if i#4 == 0 then
- Flip <- flip * -1
- end if
- end for
- end Hash
- end BundleAdditiveHash
- const FileDescription <- class File
- field KnownPeersForFile: Array.of[NopesterClient] <- Array.of[NopesterClient].empty
- field HashID : Integer %//I need to figure out how to do this bit thingy.
- field FileSize : Integer
- end File
- const FileData <- class FileData
- field Data : Array.of[Character] <- Array.of[Character].empty
- field FileSize : Integer <- 0
- field Downloaded : Array.of[Boolean] <- array.of[Boolean].empty
- end FileData
- const NopesterClient <- class nopesterClient[Server : NopesterServer, Hasher : HashFunction]
- var FileList : array.of[FileData] <- array.of[Filedata].empty
- export operation Init[FirstString : String]
- var newFile : FileData <- FileData.create
- Newfile.setData[Array.of[character].create[FirstString.length]]
- for i : Integer <- 0 while i < FirstString.length by i <- i + 1
- NewFile$Data.setElement[i, FirstString.getelement[i]]
- end for
- NewFile.setFileSize[FirstString.length]
- NewFile.setDownloaded[Array.of[Boolean].create[FirstString.length]]
- FileList.addupper[Newfile]
- stdout.PutString["\n"]
- end Init
- export function getChunk[FileHash:Integer, ChunkID:Integer] -> [Chunk:Character]
- var File : FileData <- FileList.getelement[filehash]
- if file$Downloaded.getelement[chunkID] then
- chunk <- file$Data.getelement[chunkid]
- else
- chunk <- '0'
- end if
- end getChunk
- export operation downloadFile[FileHash : Integer] -> []
- var NumDownloaded : Integer <- 0
- var RequestedFileDesc : FileDescription <- Server.requestFile[Filehash]
- %//Add check for whether the file already exists on this client. For now assume it is not here.
- var CurrFile : FileData <- FileData.create
- CurrFile$FileSize <- RequestedFileDesc$FileSize
- CurrFile$Data <- Array.of[Character].create[CurrFile$FileSize]
- CurrFile$Downloaded <- Array.of[boolean].create[CurrFile$FileSize] %//Does it start as false?
- loop exit when NumDownloaded >= CurrFile$FileSize
- var IndexToGet : integer <- -1
- for i : Integer <- 0 while i < CurrFile$FileSize by i <- i + 1
- if !Currfile$Downloaded.getelement[i] then
- IndexToGet <- i
- exit
- end if
- end for
- %//Maybe Add a check for the IndexToGet still being equal to -1.
- %//Making the assumption that IF a file is being requested, then there is at least one client currently connected that can supply the file.
- %//Otherwise this function would spin forever.
- var ClientIterator : Integer <- 1
- var RetrievedChar : Character <- RequestedFileDesc$KnownPeersForFile.getelement[ClientIterator].getChunk[RequestedFileDesc$HashID, IndexToGet]
- %//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.
- if RetrievedChar == '0' then
- ClientIterator <- ClientIterator # RequestedFileDesc$KnownPeersForFile.upperbound
- ClientIterator <- ClientIterator + 1
- else
- CurrFile$Data.setelement[IndexToGet, RetrievedChar]
- CurrFile$Downloaded.setelement[IndexToGet, true] %//Set to true to signalize that it has been downloaded.
- end if
- %//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.
- NumDownloaded <- NumDownloaded + 1
- end loop
- end downloadFile
- end nopesterClient
- const NopesterServer <- class nopesterServer
- var KnownPeers: Array.of[NopesterClient]<-Array.of[NopesterClient].empty
- var Files: Array.of[FileDescription]<-Array.of[FileDescription].empty
- export operation addFile[File:FileDescription, Uploader:NopesterClient]
- %//Maybe we should add some functionality to add all peers given by the client to the list on the server?
- var FileIndex : Integer <- -1
- %//Find the hash by looping through all known files.
- for i : Integer <- 0 while i < Files.upperbound+1 by i <- i + 1
- %//If the input file's hashid is the same as one of the files stored, then save that index.
- if Files.getelement[i]$HashID == File$HashID then
- FileIndex <- i
- exit
- end if
- end for
- if FileIndex == -1 then
- %//Add the new file description.
- Files.addupper[File]
- Files.getelement[Files.upperbound]$KnownPeersForFile.addupper[Uploader]
- else
- %//Add the client to the list for that file. Add a check for client already ebing in the list.
- Files.getelement[FileIndex]$KnownPeersForFile.addupper[Uploader]
- end if
- end addFile
- export function requestFile[FileHash:Integer] -> [File:FileDescription]
- var FileIndex : Integer <- -1
- %//Find the hash by looping through all known files.
- for i : Integer <- 0 while i < Files.upperbound+1 by i <- i + 1
- %//If the input file's hashid is the same as one of the files stored, then save that index.
- if Files.getelement[i]$HashID == File$HashID then
- FileIndex <- i
- exit
- end if
- end for
- if FileIndex == -1 then
- File <- nil
- else
- File <- Files.getelement[filehash]
- end if
- end RequestFile
- end nopesterServer
Advertisement
Add Comment
Please, Sign In to add comment