Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net._
- import java.io._
- import scala.io._
- import scala.util.control._
- import java.lang.String
- import collection.mutable.HashMap
- object ServerObject {
- class Server(val _conn: Socket) extends Runnable {
- val _connection = _conn
- var root:String = System.getProperty("user.dir")
- val in = new DataInputStream(_connection.getInputStream())
- val out = new DataOutputStream(_connection.getOutputStream())
- var currentClient = ""
- var rootFolder = slashFix(root)
- var currentDirectory = slashFix(pwd())
- def getListOfFiles(dir: String):List[File] = {
- val d = new File(dir)
- if (d.exists && d.isDirectory) {
- d.listFiles.toList
- } else {
- List[File]()
- }
- }
- def ls() : String = {
- setDir(currentDirectory)
- val Currdir = System.getProperty("user.dir")
- val files = getListOfFiles(Currdir)
- var dirs :String = "\n"
- for ( i <- 0 to (files.length - 1)) {
- dirs += '\t'+files(i).getName()
- }
- return dirs
- }
- def cd() : Unit = {
- out.writeUTF("Insert name of the directory that you want to navigate to: ")
- out.flush()
- var dir :String = in.readUTF()
- setDir(currentDirectory)
- println(dir)
- val cd = new File(slashFix(root)+"/"+dir)
- println(cd.exists)
- if(cd.exists && cd.isDirectory)
- {
- System.setProperty("user.dir", slashFix(root)+"/"+dir)
- println(System.getProperty("user.dir"))
- out.writeUTF("Current directory is: "+System.getProperty("user.dir"))
- out.flush()
- }else{
- out.writeUTF("Directory does not exist")
- out.flush()
- }
- }
- def fileInputStream(fName:String,fLen:String):Unit={
- val fos = new FileOutputStream(pwd+"/"+fName)
- var bytes = new Array[Byte](1024)
- var readBytes:Int = 0
- var fSize:Int = Integer.parseInt(fLen)
- println(fSize)
- while(fSize > 0) {
- if(fSize <= 1024){
- readBytes = in.read(bytes, 0, fSize)
- } else{
- readBytes = in.read(bytes, 0, 1024)
- }
- fSize -= readBytes
- fos.write(bytes, 0, readBytes)
- }
- fos.close()
- out.writeUTF("Received successfully")
- out.flush()
- }
- def put() : Unit = {
- out.writeUTF("Input file path")
- out.flush()
- val fileName = in.readUTF()
- val FileS = in.readUTF()
- setDir(currentDirectory)
- if(fileName != "File does not exist"){
- fileInputStream(fileName,FileS)
- }else {
- out.writeUTF("File does not exist")
- out.flush()
- }
- }
- def mkdir() : Unit = {
- out.writeUTF("Name of new directory: ")
- out.flush()
- val _dirName = in.readUTF()
- setDir(currentDirectory)
- val mkDir = new File(System.getProperty("user.dir") + "/" + _dirName)
- val successful = mkDir.mkdir();
- if(successful) {
- out.writeUTF("Directory successfully created")
- }else {
- out.writeUTF("Unable to create directory")
- }
- out.flush()
- }
- def rm() : Unit = {
- out.writeUTF("Name of file/directory to delete: ")
- out.flush()
- val delPath = in.readUTF()
- setDir(currentDirectory)
- val rm = new File(System.getProperty("user.dir") + "/" + delPath)
- if(rm.exists) {
- rm.delete()
- out.writeUTF("Deleted successfully")
- }else {
- out.writeUTF("Unable to delete")
- }
- out.flush()
- }
- def fileOutputStream(fLen:Int,requestedFile:String):Unit={
- out.writeUTF(fLen.toString())
- out.flush()
- val fos = out
- val reader = new FileInputStream(pwd()+"/"+requestedFile);
- var bytes = new Array[Byte](1024);
- var fSize:Int = fLen
- var readBytes:Int = 0
- while(fSize > 0) {
- if(fSize <= 1024){
- readBytes = reader.read(bytes, 0, fSize)
- } else{
- readBytes = reader.read(bytes, 0, 1024)
- }
- fSize -= readBytes
- fos.write(bytes, 0, readBytes)
- }
- reader.close();
- out.writeUTF("File sent successfully")
- out.flush()
- }
- def get() : Unit = {
- out.writeUTF("File to download")
- out.flush()
- val request = in.readUTF()
- val file = new File(pwd()+"/"+request)
- if(file.exists){
- fileOutputStream(file.length.intValue(),request)
- }else{
- out.writeUTF("File does not exists")
- out.flush()
- }
- }
- def slashFix(root:String):String={
- return root.replace('\\','/')
- }
- def pwd():String={
- return System.getProperty("user.dir")
- }
- def setDir(path:String):Unit={
- System.setProperty("user.dir", slashFix(path))
- }
- def userValidator(user: String, password: String) : Boolean = {
- var usuarios = HashMap[String, String]()
- val file = new File("users.txt")
- var leido = ""
- if (file.exists){
- Source.fromFile("users.txt" ).foreach{case line =>
- leido += line
- }
- var splitUsers: Array[java.lang.String] = leido.split(",").map(_.trim)
- var isUser = 0;
- var arrayTam = splitUsers.size
- while(arrayTam > isUser){
- usuarios(splitUsers(isUser)) = splitUsers(isUser+1)
- isUser += 2
- }
- }
- if(usuarios contains user){
- if(usuarios(user) == password){
- println("successful login")
- return true
- }else{
- println("Wrong Password")
- return false
- }
- }else {
- out.writeUTF("User not registered, create user? [Y/N]")
- if(in.readUTF()=="Y"){
- usuarios(user) = password
- println("Successfully registered user, you are now logged in")
- val mkDir = new File(System.getProperty("user.dir") + "/" + user)
- mkDir.mkdir();
- val writer = new PrintWriter(new File("users.txt" ))
- var FileS = usuarios.size
- var iteracion = 0;
- usuarios.foreach{case(key, value) =>
- writer.write(key)
- writer.write(",")
- writer.write(value)
- if(iteracion != (FileS-1)){
- writer.write(",")
- }
- iteracion += 1;
- }
- writer.close()
- return true
- }else{
- return false
- }
- }
- return false
- }
- def printLogin():String={
- var login:String = "\n"
- login+="\t"+"*********************************"+"\n"
- login+="\t"+"***-----------Login-----------***"+"\n"
- login+="\t"+"*** Input you username: ***"+"\n"
- login+="\t"+"*********************************"
- return login
- }
- def validateLogin():Unit={
- }
- def logic() : Unit = {
- var isActive:Boolean = true
- while(isActive){
- out.writeUTF(printLogin)
- var userName:String= in.readUTF()
- out.writeUTF("Input you password: ")
- var password:String= in.readUTF()
- if(userValidator(userName,password)){
- out.writeUTF("Successful login")
- System.setProperty("user.dir", slashFix(root)+"/"+userName)
- while(true){
- if(currentClient == ""){
- currentClient = userName
- if(currentClient != ""){
- rootFolder = pwd()
- currentDirectory = pwd()
- }
- }else{
- val received = in.readUTF()
- println("Message Received:" + received)
- if(received == "pwd"){
- setDir(currentDirectory)
- out.writeUTF(pwd())
- out.flush()
- }else if (received == "ls"){
- setDir(currentDirectory)
- out.writeUTF(ls())
- out.flush()
- }else if(received == "cd"){
- setDir(currentDirectory)
- cd()
- }else if(received == "get"){
- setDir(currentDirectory)
- get()
- out.writeUTF("")
- }else if(received == "put"){
- setDir(currentDirectory)
- put()
- }else if(received == "rm"){
- setDir(currentDirectory)
- rm()
- }else if(received == "mkdir"){
- setDir(currentDirectory)
- mkdir()
- }else if(received == "exit"){
- out.writeUTF(received)
- out.flush
- _connection.close()
- isActive = false
- }else{
- out.writeUTF("Invalid command")
- out.flush()
- }
- }
- }
- currentDirectory = pwd()
- }else{
- out.writeUTF("Invalid User/Password")
- out.flush()
- }
- }
- }
- def run {
- logic()
- }
- }
- def main(args: Array[String]){
- val socket = new ServerSocket(8000)
- while (true) {
- val sock = socket.accept()
- println(sock.getRemoteSocketAddress())
- new Thread(new Server(sock)).start
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement