示例#1
0
        int UploadAssetPackage()
        {
            SendProtocolCode(PROTOCOL_CODES.ACCEPT); //TODO add logic to check if the upload file is too large
            string fileName = ReceiveMessage();

            SendMessage(generateSASkeytoUpload(username, fileName));
            PROTOCOL_CODES reply = receiveProtocolCode();


            if (reply == PROTOCOL_CODES.OK)
            {
                if (db.UploadAssetPackage(username, fileName))
                {
                    SendProtocolCode(PROTOCOL_CODES.OK);
                    return(1); // all went fine
                }
                else
                { //something went wrong with adding data to the database
                    SendProtocolCode(PROTOCOL_CODES.ERROR_NO_DBCONNECTION);
                    return(-1);
                }
            }
            else if (reply == PROTOCOL_CODES.ERROR)
            {//something is wrong with the sas link or upload..
            }
            return(-1);
        }
示例#2
0
 PROTOCOL_CODES sendRequest(PROTOCOL_CODES code)
 {
     if (clientSocket == null)
     {
         return(PROTOCOL_CODES.ERROR);
     }
     try
     {
         // Get a stream object for writing.
         if (stream.CanWrite)
         {
             byte[] message = BitConverter.GetBytes((int)code);
             stream.Write(message, 0, 4);
             stream.Read(bytesFrom, 0, 4); //read the replycode
             Int32 reply = BitConverter.ToInt32(bytesFrom, 0);
             Console.WriteLine("Client sent request. Received reply:" + ((PROTOCOL_CODES)reply).ToString());
             return((PROTOCOL_CODES)reply);
         }
     }
     catch (SocketException socketException)
     {
         Console.WriteLine("Socket exception: " + socketException);
         status = STATUS.ERROR;
         return(PROTOCOL_CODES.ERROR);
     }
     return(PROTOCOL_CODES.ERROR);
 }
示例#3
0
 bool sendProtocolCode(PROTOCOL_CODES code)
 {
     if (clientSocket == null)
     {
         return(false);
     }
     try
     {
         // Get a stream object for writing.
         if (stream.CanWrite)
         {
             byte[] message = BitConverter.GetBytes((int)code);
             stream.Write(message, 0, 4); //read the replycode
             Console.WriteLine("Sent protocol code:" + ((PROTOCOL_CODES)code).ToString());
             return(true);
         }
         returnCode = -1;
         status     = STATUS.ERROR;
         return(false);
     }
     catch (SocketException socketException)
     {
         Console.WriteLine("Socket exception: " + socketException);
         returnCode = -1;
         status     = STATUS.ERROR;
         return(false);
     }
 }
示例#4
0
 private void serveClient()
 {
     while ((true))
     {
         try
         {
             while (true)
             {
                 PROTOCOL_CODES code = getRequest();
                 requestCount = requestCount + 1;
                 int requestResult = handleRequest(code);
                 if (requestResult == 0)
                 { //client wanted to quit
                     return;
                 }
                 if (requestResult == -1 && status == STATUS.ERROR)
                 {//error in handling request that wasent trivial
                     return;
                 }
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine("Client " + clientName + "experienced error!: " + ex.ToString());
             returnCode = -1;
             status     = STATUS.ERROR;
         }
     }
     returnCode = 0;
     status     = STATUS.QUIT;
 }
示例#5
0
        private void ServeClient()
        {
            while ((true))
            {
                try
                {
                    while (true)
                    {
                        PROTOCOL_CODES code = receiveProtocolCode();
                        requestCount = requestCount + 1;

                        if (code == PROTOCOL_CODES.ERROR)
                        {
                            server.removeClient(clientName);
                            Console.WriteLine(clientName + " encountered error!");
                            return;
                        }
                        int requestResult = HandleRequest(code);
                        if (requestResult == 0)
                        { //client wanted to quit
                            server.removeClient(clientName);
                            Console.WriteLine(clientName + " has quit! ");
                            writer.Flush();
                            reader.Close();
                            writer.Close();
                            pingSocket.Close();
                            clientSocket.Close();
                            return;
                        } //requestResult == -1 &&
                        if (status == STATUS.ERROR)
                        {//error in handling request that wasnt trivial
                            server.removeClient(clientName);
                            Console.WriteLine(clientName + " has crashed!");
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Client " + clientName + "experienced error!: " + ex.ToString());
                    returnCode = -1;
                    status     = STATUS.ERROR;
                }
            }
            returnCode = 0;
            status     = STATUS.QUIT;
        }
示例#6
0
 PROTOCOL_CODES SendRequest(PROTOCOL_CODES code)
 {
     try
     {
         // Get a stream object for writing.
         writer.Write((Int32)code);
         writer.Flush();
         //read the replycode
         Int32 reply = reader.ReadInt32();;
         Console.WriteLine("Client sent request. Received reply:" + ((PROTOCOL_CODES)reply).ToString());
         return((PROTOCOL_CODES)reply);
     }
     catch (Exception socketException)
     {
         Console.WriteLine("Socket exception: " + socketException);
         status = STATUS.ERROR;
         return(PROTOCOL_CODES.ERROR);
     }
     return(PROTOCOL_CODES.ERROR);
 }
示例#7
0
        bool SendProtocolCode(PROTOCOL_CODES code)
        {
            try
            {
                // Get a stream object for writing.

                writer.Write((Int32)code);
                writer.Flush();
                Console.WriteLine("Sent protocol code:" + ((PROTOCOL_CODES)code).ToString());

                return(true);
            }
            catch (Exception socketException)
            {
                Console.WriteLine("Socket exception: " + socketException);
                returnCode = -1;
                status     = STATUS.ERROR;
                return(false);
            }
        }
示例#8
0
        //0 is quit -1 error 1 is ok
        int HandleRequest(PROTOCOL_CODES request)
        {
            Int32 bytesToCome;

            switch (request)
            {
            case PROTOCOL_CODES.UPLOAD_ASSETPACKAGE:
                return(UploadAssetPackage());

            case PROTOCOL_CODES.SENDIMAGE:
                return(HandleSendimage());

            //loput samalla tavalla

            case PROTOCOL_CODES.GET_MY_CONTENTPACKS:
                if (FetchContentPacksByUser(username))
                {
                    return(1);
                }
                return(-1);

            case PROTOCOL_CODES.GET_SERIES_IN_PACKAGE:
                SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                //read how many bytes are incoming
                bytesToCome = reader.ReadInt32();
                if (bytesToCome < requestMaxSize)
                {
                    SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                    Byte[] received = ReceiveBytes(bytesToCome);
                    if (FetchVideoSeriesInPackage(Encoding.UTF8.GetString(received)))
                    {
                        return(1);
                    }
                    return(-1);
                }
                else
                {
                    SendProtocolCode(PROTOCOL_CODES.DENY);
                    return(1);
                }

            case PROTOCOL_CODES.GET_VIDEOS_IN_SERIES:
                SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                //read how many bytes are incoming
                bytesToCome = reader.ReadInt32();
                if (bytesToCome < requestMaxSize)
                {
                    SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                    Byte[] received = ReceiveBytes(bytesToCome);
                    if (FetchVideoSeriesInPackage(Encoding.UTF8.GetString(received)))
                    {
                        return(1);
                    }
                    return(-1);
                }
                else
                {
                    SendProtocolCode(PROTOCOL_CODES.DENY);
                    return(1);
                }

            /**
             * HERE IS WHERE YOUR CODE IS NEEDED AND SIMPLE USE CASE INTEGRATES IN
             *
             *
             **/
            case PROTOCOL_CODES.UPLOAD_VIDEO:
                SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                //read how many bytes are incoming (size of videos name)
                bytesToCome = reader.ReadInt32();
                string filename = Encoding.UTF8.GetString(ReceiveBytes(bytesToCome));

                //TODO logic to check if user would go over his upload limit

                //--------------------------------------------------------------------------------
                //TODO:
                //generate the SAS link where the client application can upload the video
                //(the link should be limited to work only for that certain IP where the request comes from, and only work for a certain amount of time)
                //
                //we add all the nesserary stuff to generate SAS access links for viewing the video by others to the database into the videos "data field" which is just a byte array
                //
                //send the SAS link to the client -- that application then handles the actual uploading of the video the azure storage
                ////--------------------------------------------------------------------------------



                return(-1);

            case PROTOCOL_CODES.POST_EDITS:

                SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                SendBytes(Encoding.UTF8.GetBytes(generateSASkeytoWatch("robert", "SAM_100_0131.mp4")));

                return(1);


            case PROTOCOL_CODES.REQUEST_VIEW_VIDEO:



                SendProtocolCode(PROTOCOL_CODES.ACCEPT);

                string msg = ReceiveMessage();
                Console.WriteLine(msg);
                SendBytes(Encoding.UTF8.GetBytes(generateSASkeytoWatch(msg.Split('/')[0], msg.Split('/')[1])));

                //todo sivu videoiden lähettäminen

                return(1);

            /*SendProtocolCode(PROTOCOL_CODES.ACCEPT);
             *
             * string videoID = ReceiveMessage(); //user sends the ID of the video he wants to view
             *
             * byte[][] videoData = db.getVideoData(videoID); //we get the data that is needed to generate the SAS key that lets user view it
             *
             * string[] reply;
             * if (videoData[0].Length == 0)
             * {
             *  SendListString(null);
             *  return 1; //if the video is not found in the database return 0 its fiiine...
             * }
             *
             *
             * string uploader = System.Text.Encoding.UTF8.GetString(videoData[0]);
             * string sideVideoLeft = System.Text.Encoding.UTF8.GetString(videoData[1]);
             * string sideVideoRight = System.Text.Encoding.UTF8.GetString(videoData[2]);
             *
             *
             * if (sideVideoLeft.Length > 0 || sideVideoRight.Length > 0)
             * {//if either of the side videos exist the lenght of the reply array is 3
             *  reply = new string[3];
             * }
             * else
             * {
             *  reply = new string[1];
             * }
             *
             * reply[0] = generateSASkeytoWatch(uploader, videoID);
             *
             * if (sideVideoLeft.Length > 0)
             * {
             *  reply[1] = generateSASkeytoWatch(uploader,sideVideoLeft);
             * }
             * if (sideVideoRight.Length > 0)
             * {
             *  reply[2] = generateSASkeytoWatch(uploader, sideVideoRight);
             * }
             *
             * SendListString(reply);
             * return 0;
             */
            case PROTOCOL_CODES.SENDJSON:
                SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                //read how many bytes are incoming
                //bytesToCome = reader.ReadInt32();
                string data = reader.ReadString();
                try
                {
                    Console.WriteLine("got reply: " + data);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                /*if (bytesToCome < requestMaxSize)
                 * {
                 *  SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                 *  Byte[] received = ReceiveBytes(bytesToCome);
                 *  Console.WriteLine(System.Text.Encoding.UTF8.GetString(received));
                 * }
                 * else
                 * {
                 *  SendProtocolCode(PROTOCOL_CODES.DENY);
                 * }*/
                return(1);

            case PROTOCOL_CODES.QUIT:
                //SendProtocolCode(PROTOCOL_CODES.ACCEPT);
                bytesToCome = 0;
                return(0);

            default:
                SendProtocolCode(PROTOCOL_CODES.ERROR);
                Console.WriteLine("Cannot handle request: " + ((PROTOCOL_CODES)request).ToString());
                return(-1);
            }
        }
示例#9
0
        //0 is quit -1 error 1 is ok
        int handleRequest(PROTOCOL_CODES request)
        {
            Int32  bytesToCome;
            string sasLink;

            switch (request)
            {
            case PROTOCOL_CODES.SENDIMAGE:
                return(handleSendimage());

            //loput samalla tavalla

            case PROTOCOL_CODES.GET_MY_CONTENTPACKS:
                if (fetchContentPacksByUser(username))
                {
                    return(1);
                }
                return(-1);

            case PROTOCOL_CODES.GET_SERIES_IN_PACKAGE:
                sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                stream.Read(bytesFrom, 0, 4);     //read how many bytes are incoming
                bytesToCome = BitConverter.ToInt32(bytesFrom, 0);
                if (bytesToCome < requestMaxSize)
                {
                    sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                    Byte[] received = receiveBytes(bytesToCome);
                    if (fetchVideoSeriesInPackage(Encoding.UTF8.GetString(received)))
                    {
                        return(1);
                    }
                    return(-1);
                }
                else
                {
                    sendProtocolCode(PROTOCOL_CODES.DENY);
                    return(1);
                }

            case PROTOCOL_CODES.GET_VIDEOS_IN_SERIES:
                sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                stream.Read(bytesFrom, 0, 4);     //read how many bytes are incoming
                bytesToCome = BitConverter.ToInt32(bytesFrom, 0);
                if (bytesToCome < requestMaxSize)
                {
                    sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                    Byte[] received = receiveBytes(bytesToCome);
                    if (fetchVideoSeriesInPackage(Encoding.UTF8.GetString(received)))
                    {
                        return(1);
                    }
                    return(-1);
                }
                else
                {
                    sendProtocolCode(PROTOCOL_CODES.DENY);
                    return(1);
                }

            /**
             * HERE IS WHERE YOUR CODE IS NEEDED AND SIMPLE USE CASE INTEGRATES IN
             *
             *
             **/
            case PROTOCOL_CODES.UPLOAD_VIDEO:
                sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                stream.Read(bytesFrom, 0, 4);     //read how many bytes are incoming (size of videos name)
                bytesToCome = BitConverter.ToInt32(bytesFrom, 0);
                string filename = Encoding.UTF8.GetString(receiveBytes(bytesToCome));

                //TODO logic to check if user would go over his upload limit

                //--------------------------------------------------------------------------------
                //TODO:
                //generate the SAS link where the client application can upload the video
                //(the link should be limited to work only for that certain IP where the request comes from, and only work for a certain amount of time)
                //
                //we add all the nesserary stuff to generate SAS access links for viewing the video by others to the database into the videos "data field" which is just a byte array
                //
                //send the SAS link to the client -- that application then handles the actual uploading of the video the azure storage
                ////--------------------------------------------------------------------------------

                sasLink = UploadToAzure(bytesFrom, filename);

                return(-1);

            case PROTOCOL_CODES.REQUEST_VIEW_VIDEO:
                sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                stream.Read(bytesFrom, 0, 4);
                string videoID = Encoding.UTF8.GetString(bytesFrom); //user sends the ID of the video he wants to view

                byte[] videoData = db.getVideoData(videoID);         //we get the data that is needed to generate the SAS key that lets user view it

                if (videoData != null)
                {
                    //--------------------------------------------------------------------------------
                    //TODO:
                    //generate the SAS link from the video data, that is only viewable by that clients IP for a certain amount of time
                    //
                    //we add all the nesserary stuff to generate SAS access links for viewing the video by others to the database
                    //
                    //send the SAS link to the client -- that application then handles the
                    ////--------------------------------------------------------------------------------
                    sasLink = UploadToAzure(videoData, videoID);
                }

                return(-1);


            case PROTOCOL_CODES.SENDJSON:
                sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                Console.WriteLine("awaiting reply");
                stream.Read(bytesFrom, 0, 4);     //read how many bytes are incoming
                bytesToCome = BitConverter.ToInt32(bytesFrom, 0);
                Console.WriteLine("got reply");
                if (bytesToCome < requestMaxSize)
                {
                    sendProtocolCode(PROTOCOL_CODES.ACCEPT);
                    Byte[] received = receiveBytes(bytesToCome);
                }
                else
                {
                    sendProtocolCode(PROTOCOL_CODES.DENY);
                }
                return(1);

            case PROTOCOL_CODES.QUIT:
                return(0);


            default:
                sendProtocolCode(PROTOCOL_CODES.ERROR);
                Console.WriteLine("Cannot handle request: " + ((PROTOCOL_CODES)request).ToString());
                return(-1);
            }
        }