Sunday 9 June 2013

Violin metronome

For this piece I'm working on my teacher really wants me to focus on the rit. during this one phrase since I'm not slowing down enough, I'm only really doing the decrescendo. So I decided to write a little metronome program that uses my laptop's PC beep to mark the rhythm and using sleep to create the tempo. Then once it hits the rit. in the phrase I gradually increase the amount of time that it sleeps.

 Here's the source code:

#include <cstdio>
#include <Windows.h>
int main(){
  int sleep=850; // In milliseconds
  for(int beat=0; beat<=15;beat++){
    if(beat>11){
      sleep+=500;
    }
  printf("\a");
  Sleep(sleep);
  }
  printf("\a"); // Just as a reminder to mark the end of the fourth beat
}

Saturday 8 June 2013

SSH Tunneling

Last week I managed to configure OpenSSH(1) on my Arch server at home, hostname bluebeetle, to act as a SOCKS(2) tunnel so I could do a little more secure browsing when I'm at school, I used a Bash function(3) to launch the bound proxy in chromium on my laptop, hostname WhiskeyJack. This week I got a profile set up on my W7 box, hostname The-Mage, in PuTTY to connect through the SOCKS tunnel. While I was doing that I had the idea of seeing if I could use the tunnel to not only redirect my HTTP traffic through the tunnel but also if I could use the tunnel to connect low-level to the PostgreSQL server running on bluebeetle. After much tinkering around and a little goggling, plus a couple brain farts, I got the tunnel set up properly and I got the pgsql program I was writing to connect. Hint: it took longer than I care to admit that I should have changed bluebeetle's address in the source code from it's LAN address to 'localhost' since the tunnel was doing the routing not my router.

Friday 29 March 2013

Boost

After class the other day a question had been raised about network programming, I've used Boost::asio in the past for my own network programming tasks. Boost is a third party library pack, designed for platform independence, that contains libraries for a large majority of common tasks. Boost ASIO is their (A)Synchronous In Out library that contains support for, among other tasks, TCP/UDP programming. In order to implement full duplex(asynchronous) communication you also need to implement threading to allow the reading and writing handles to run on separate threads to prevent them from blocking one another - synchronous communication. Boost provides thread management support in their Boost::thread library.

http://www.boost.org/

Bits and Bytes

I completed these a while ago and but forgot to post it. I haven't completed the Bits class yet but I have finished all the functions.


void copyBits(unsigned int& V, int bitNo, int NoOfBits, unsigned int mask){
  unsigned int m;
  for(int x=1, m=1<<bitNo; x<=NoOfBits; x++, m<<=1){
    setBit(V,(bitNo+x), (mask&m)&&1);
  }
}
void setBit(unsigned int& V, int bitNo, bool value){
  if(value){
    V = V | (1<<(bitNo-1));
    }
  else{
    V = V & ~(1<<(bitNo-1));
    }
}
void prnBits(unsigned int val){
  unsigned int m = 1 << sizeof(val)*8-1;
  int count=0;
  while(m){
    if(count==4){
      printf(" ");
      count=0;
    }
    printf("%d", (val & m)&&1);
    m = m >> 1;
    count++;
  }
}
const char* bits(unsigned int val){
  const unsigned int valSize=sizeof(val)*8-1;
  char* output = new char[valSize+2];
  unsigned int m = 1 << valSize;
  int x=0;
  for(x=0;x<=valSize;x++, m = m >> 1){
    output[x]=(val & m ? '1' : '0');
  }
  output[x]='\0';
  return output;
}
void bitDump(void* address, unsigned int sizeInBytes){
  unsigned long long valSize=(sizeInBytes*8)-1;
  unsigned long m = 1 << valSize;
  long long temp = *(reinterpret_cast<long long *>(address));
  int count=0;
  unsigned long x=0;
  printf("\n");
  for(x=0;x<=valSize,m;x++, m = m >> 1){
    if(count==4){
      printf(" ");
      count=0;
    }
    printf("%d", ((temp) & m)&&1);
    count++;
  }
}

Thursday 28 March 2013

0.5 CDialog issue

I noticed this when testing cmenuitem, when the following code is run it checks the address of the returning variable against the current pointer in the array. The thing is though that the return variable is a reference to a pointer object, I debugged massively the incoming and outgoing addresses in CDialog's add and I know for a fact that the proper address is stored within CDialog _fld but since curField() returns a reference I don't know how to get it to check against the proper address of the pointer it is referencing.

// Test7MenuItem.cpp

 for(int i=0;i<3;i++){
         if(&FD.curField() == m[i]){
            si[16] = '0' + i;
            FD[3].set(si);
          }

//cdialog.cpp

 CField& CDialog::curField(){
    return (CField&)_fld[_curidx];
  }

this

One thing I've noticed a lot of people asking is about the constructors asking to instantiate property _Label's container/frame. The hint that Fardad gave was (i.e. this). It's saying that the owner class should be set to the frame of the _Label, the code for that would be:  _Label.frame(this); since this is a pointer to the class it would pass the class to _Label's frame.

Saturday 26 January 2013

Week 3 To Do's

For the Xmover program, the solution I wrote is a total kludge. If I come up with something a little more elegant I'll post that code later.


case DOWN:
 if(row < console.getRows()-1){
 if(row == console.getRows()-2 && col == console.getCols()-1){
 console.alarm();
 }
 else{
 row++;
 }
      }
      else{
        console.alarm();
      }
      break;


case RIGHT:
      if(col < console.getCols()-1){
 if(col == console.getCols()-2 && row == console.getRows()-1){
 console.alarm();
 }
 else{
col++;
 }
      }
      else{
        console.alarm();
      }
      break;


For removing the 'if' from the ::display() function and reducing it to one line I once again wrote a total kludge, it is completely confusing to look at and it's ugly but dang it it works.


  void Console::display(const char* str, int row, int col, int fieldLen){
        setPos(row, col);
int i;
for(i=0,fieldLen==0?fieldLen=strlen(str):i=0;i<fieldLen;str[i]!=0?putChar(str[i]):putChar(' '),i++);
  }

This code also makes it possible to display any characters that you may insert past the first NULL char in the array when you are scrolling through str.


Monday 21 January 2013

prnenv v2


Here is the updated code that prints out a list of suggested matches: All the updated code has already been pushed to the repo if you wish to view the full code.

                    else{
/* If the search is not a complete match,
  then it prints out a list of partial matches.
*/
cout << "That environmental variable is not found." << endl;
cout << "Did you mean:" << endl;
for(x=0;env[x]!=0;x++){
pch=strstr(env[x], str);
if(pch){
pch2 = NULL;
pch2 = strtok (env[x], "=");
if(pch2)
cout << pch << endl;
} // End of if
} // End of for
// Resets result variable back to original size to allow proper deletion.
strcpy(result, env[y]);
delete [] result;
} // End of else

Saturday 19 January 2013

Week 2 CL exercises

I've set up a github repo where I will be saving all my exercises through the semester to. I'm doing this as practice to get used to using git as well as the fact that I'm bouncing around four different OS's as well as a couple of different development set-ups.

https://github.com/Zardvark/OOP-344-Exercises is the main Repo link

Here are the first four CL programs:
add.cpp
div.cpp
mul.cpp
sub.cpp

And here is the prnenv.cpp program, after I finish my INT lab I will go back and attempt add on a "Did you mean:" search function to the prnenv program, if I get it working I will post the code directly to this blog

Saturday 12 January 2013

Chronic Misadventures

So I had decided to get an Arch server set up on an old tower to experiment with git, Apache, php, MySQL, among other things. I'm doing this on another Arch box so as not to clutter my Arch box installed on my laptop. It was also to re-familiarise myself with the Arch install process for when I start working on my Arch presentation for the Linux and Android Club.

I go ahead and get my old tower dusted off and plug everything back in but it doesn't want to boot. After an hour of fiddling around with parts and plugs and mix-and-matching components from two different towers I finally get the thing booting.

So after stumbling my way through it for an hour, I got Arch installed and configured. This was much much better then last time I installed Arch and I didn't use the guides. I get Arch installed and configured. I run the GRUB installer and get GRUB configured. I reboot and GRUB is now hanging... why?!?! I pop the installer back in, arch-chroot back into my fresh install and for some reason the linux.img and initramfs.img had gotten wiped by the GRUB install - mkinitcpio -p linux failing horribly was my clue >> . Reinstall Linux - too tired at this point to create the linux.img manually, rerun the GRUB config script - it finds the linux.img, success! and reboot. Huzzah! Its working.

Fast forward a couple hours later, OpenSSH is configured and I get my SSH key set up, everything is running smoothly. Reboot the server and switch back to Windows on my Laptop. SSH isn't working any more *scratch scratch*, 'Connection Refused' CentOS is telling me, 'Connection Terminated' PuTTY is telling me. Turns out systemd had forgotten to enable and start dhcpcd, ugh, took me twenty minutes to figure that out and only because I tried running pacman to install the internet utilities package.

It's now dinner time, I'm enjoying my meal and catching up on some TV on my laptop. I go to change the volume and knock over my tiny glass. SPLASH! it gets all over my keyboard, I turn my laptop off and upside down and let it drain. I check back a while later and I've short circuited my keyboard, yay! So now I sit here typing on a temporary rubber/soft-touch keyboard with a big hole in my laptop case where the keyboard goes.