private void start_listening()
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 11111);
            Socket     newsock       = new Socket(AddressFamily.InterNetwork,
                                                  SocketType.Stream, ProtocolType.Tcp);

            newsock.Bind(localEndPoint);
            newsock.Listen(10);

            Console.WriteLine("participant started listening..");
            while (true)
            {
                Socket client    = newsock.Accept();
                byte[] remotehdr = new byte[128];

                client.Receive(remotehdr);
                RFSCommHeader hobj = new RFSCommHeader();
                hobj.bytestream_to_headerobj(remotehdr, 0);
                curr_drive_id = hobj.driveid;
                long seqn = hobj.seq_num;
                Console.WriteLine("recived new connection attempt");

                if (hobj.optype == RCS_OP.CONNECT)
                {
                    bool alreadymounted = false;
                    long drivesize      = 0;
                    bool drivepresent   = check_if_driveid_exists(hobj.driveid, ref alreadymounted, ref drivesize);

                    if (drivepresent == true && alreadymounted == false)
                    {
                        byte[] replyhdr = new byte[128];
                        Prepare_Header2(replyhdr, RCS_OP.CONNECT_ACK_OKAY, 1, drivesize, 0, seqn);
                        client.Send(replyhdr);

                        Lun_Item ci = create_new_conn_entry(hobj.driveid, client);
                        Thread   t  = new Thread(start_servicing);
                        t.Start(ci);
                        Console.WriteLine("sent new connection okay");
                    }
                    else
                    {
                        byte[] replyhdr = new byte[128];
                        Prepare_Header2(replyhdr, RCS_OP.CONNECT_ACK_FAIL, 1, 0, 0, seqn);
                        client.Send(replyhdr);
                        client.Close();
                        Console.WriteLine("sent new connection failed");
                    }
                }
            }
        }
 public bool check_if_driveid_exists(int id, ref bool alreadymounted, ref long disksize)
 {
     lock (m_lunlist)
     {
         for (int i = 0; i < m_lunlist.Count; i++)
         {
             Lun_Item li = m_lunlist.ElementAt(i);
             if (li.drive_id == id)
             {
                 alreadymounted = (li.client != null) ? true : false;
                 disksize       = li.drive_size;
                 return(true);
             }
         }
     }
     return(false);
 }
 private void mark_end_connection(int driveid)
 {
     lock (m_lunlist)
     {
         for (int i = 0; i < m_lunlist.Count; i++)
         {
             Lun_Item li = m_lunlist.ElementAt(i);
             if (li.drive_id == driveid)
             {
                 li.client        = null;
                 li.drive_state   = "-";
                 li.remote_ip     = "-";
                 refresh_required = true;
             }
         }
     }
 }
 private Lun_Item create_new_conn_entry(int driveid, Socket c)
 {
     lock (m_lunlist)
     {
         for (int i = 0; i < m_lunlist.Count; i++)
         {
             Lun_Item li = m_lunlist.ElementAt(i);
             if (li.drive_id == driveid)
             {
                 li.client        = c;
                 li.drive_state   = "mounted";
                 li.remote_ip     = c.RemoteEndPoint.ToString();
                 refresh_required = true;
                 return(li);
             }
         }
     }
     DEFS.ASSERT(false, "Something went wrong in new conn");
     return(null);
 }
        /*
         * Just get the list of all files and their properties. If a new lun is created
         * then the list must be reloaded. After the list is reloaded, then it can be displayed
         * on the UI.
         */
        public void load_lun_list(bool freshload)
        {
            Inode_Info[] inodes = REDDY.ptrIFSDMux.FindFilesInternalAPI(1, "\\");
            DEFS.DEBUG("lun", "Found " + inodes.Length + " luns in load_lun_list");
            lock (m_lunlist)
            {
                if (freshload)
                {
                    DEFS.ASSERT(m_lunlist.Count == 0, "some Lun_Items cannot already exist");
                }
                for (int i = 0; i < inodes.Length; i++)
                {
                    DEFS.DEBUG("lun", inodes[i].name);
                    bool exists = false;
                    if (freshload)
                    {
                        exists = false;
                    }
                    else
                    {
                        //find out..
                        for (int j = 0; j < m_lunlist.Count; j++)
                        {
                            try
                            {
                                Lun_Item li  = m_lunlist.ElementAt(j);
                                int      did = Int32.Parse(inodes[i].name);

                                if (li.drive_id == did)
                                {
                                    exists = true;
                                    break;
                                }
                            }
                            catch (Exception extp)
                            {
                                DEFS.DEBUG("lun", "Error in filename from LUNdisk");
                                DEFS.DEBUG("lun", "Exception:" + extp.Message);
                                exists = true; //just to skip adding this.
                            }
                        }
                    }

                    if (!exists)
                    {
                        //create item and insert.
                        try
                        {
                            Lun_Item li = new Lun_Item();
                            li.drive_id   = Int32.Parse(inodes[i].name);
                            li.drive_size = inodes[i].size;
                            li.ctime      = inodes[i].CreationTime.Ticks;
                            m_lunlist.AddFirst(li);
                        }
                        catch (Exception ep)
                        {
                            DEFS.DEBUG("lun", ep.Message);
                        }
                    }
                }
            }//lock
        }
        /*
         * Accept requests for any drive ID serially, and reply back to them in the
         * tcp port. This runs as a new thread.
         */
        private void start_servicing(object parameter1)
        {
            Lun_Item ci     = (Lun_Item)parameter1;
            Socket   client = ci.client;

            byte[] remote_hdr = new byte[128];
            byte[] my_hdr     = new byte[128];

            while (true)
            {
                RFSCommHeader hobj = new RFSCommHeader();
                long          seqn = 0;
                try
                {
                    client.Receive(remote_hdr);
                    hobj.bytestream_to_headerobj(remote_hdr, 0);
                    seqn = hobj.seq_num;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Encountered error : " + e.Message);
                    return;
                }
                switch (hobj.optype)
                {
                case RCS_OP.NO_OP:
                    break;

                case RCS_OP.DISCONNECT:
                    Console.WriteLine("Client has disconnected!");
                    mark_end_connection(hobj.driveid);
                    client.Close();
                    return;

                case RCS_OP.READ:

                    Prepare_Header2(my_hdr, RCS_OP.READ_REPLY, hobj.driveid, hobj.driveoffset, hobj.datasize, seqn);
                    client.Send(my_hdr);

                    uint   sxsize = 0;
                    byte[] tmpbuf = new byte[hobj.datasize];

                    string fname = hobj.driveid.ToString();
                    REDDY.ptrIFSDMux.ReadFile(1, fname, tmpbuf, ref sxsize, hobj.driveoffset, null);

                    int send_ds = 0;
                    while (send_ds < hobj.datasize)
                    {
                        int sx = client.Send(tmpbuf, send_ds, hobj.datasize - send_ds, SocketFlags.None);
                        send_ds += sx;
                    }
                    break;

                case RCS_OP.WRITE:

                    Prepare_Header2(my_hdr, RCS_OP.WRITE_ACK, hobj.driveid, hobj.driveoffset, hobj.datasize, seqn);
                    int    recv_ds = 0;
                    byte[] tmpbuf2 = new byte[hobj.datasize];

                    while (recv_ds < hobj.datasize)
                    {
                        int sx = client.Receive(tmpbuf2, recv_ds, hobj.datasize - recv_ds, SocketFlags.None);
                        recv_ds += sx;
                    }

                    uint   sxsize2 = 0;
                    string fname2  = hobj.driveid.ToString();
                    REDDY.ptrIFSDMux.WriteFile(1, fname2, tmpbuf2, ref sxsize2, hobj.driveoffset, null);

                    client.Send(my_hdr);
                    break;
                    //assert otherwise
                }
            }
        }