This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiClient.cpp
143 lines (130 loc) · 3.44 KB
/
MidiClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "MidiClient.h"
MidiClient::MidiClient(string ip_addr, int portno){
this->ip_address = ip_addr;
this->serv_portno = portno;
this->clearBuffer();
}
void MidiClient::startClient(){
int result = 0;
cout << "starting client"<<endl;
cout << "ip: "<< this->ip_address << endl;
cout << "server port: "<< this->serv_portno <<endl;
result = this->establishConnection();
if(result < 0) return;
cout << "connection established" <<endl;
result = this->sendGreeting();
if(result < 0) return;
cout << "greeting sent succesfully"<<endl;
result = this->configureMidiPort();
if(result < 0) return;
cout << "midi port successfully set" << endl;
while(result == 0){
result = listenToMidiInput();
}
}
void MidiClient::clearBuffer(){
memset(this->buffer, 0, this->buffer_size);
}
int MidiClient::establishConnection(){
this->sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sock_fd < 0){
cout << "error creating socket file descriptor"<<endl;
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(serv_portno);
if(inet_pton(AF_INET, ip_address.c_str(), &serv_addr.sin_addr) < 0){
cout << "connection to server failed"<<endl;
return -1;
}
if( connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
cout << "Connection failed!"<<endl;
return -1;
}
cout << "established connection to " <<this->ip_address<<endl;
return 0;
}
int MidiClient::sendGreeting(){
//upon connection server sends value
valread = read(sock_fd, buffer, buffer_size);
cout << valread << endl;
cout << buffer<<endl;
//send back our greeting to server
send(sock_fd, "hello from client\n", 18, 0);
//after greeting get info connection
clearBuffer();
valread = read(sock_fd, buffer, buffer_size);
cout << "Midi Ports available from server"<<endl;
for(int i=0; i < valread && i < (int)buffer_size; i++){
if(buffer[i]=='#'){
cout << endl;
}
cout << buffer[i];
}
cout << endl;
return 0;
}
int MidiClient::configureMidiPort(){
bool configured = false;
string userInput = "";
while(!configured){
cout << "#:";
cin >> userInput;
send(sock_fd, userInput.c_str(), userInput.size(), 0);
clearBuffer();
valread = read(sock_fd, buffer, buffer_size);
if(isSuccess(valread)){
cout << "midi port configured" <<endl;
configured = true;
} else {
cout << "error binding midi port"<<endl;
for(int i=0; i < valread && i < (int)buffer_size; i++){
if(buffer[i]=='#'){
cout << endl;
}
cout << buffer[i];
}
cout << endl;
configured = false;
}
}
return 0;
}
int MidiClient::listenToMidiInput(){
if(!sock_fd){
cout << "no connection to server?"<<endl;
return -1;
}
clearBuffer();
valread = read(sock_fd, buffer, buffer_size);
if(!valread){
cout << "no data in valread, did server get disconnected?" <<endl;
return -1;
}
//cout << "Valread: "<<valread<<endl;
//read each value from buffer as integer to make it more readable
//ensure that we don't read outside buffer limits or outside valread limit
for(int i = 0; i < valread && i < (int)buffer_size; i++){
cout << (int)buffer[i]<< ' ';
}
cout << endl;
return 0;
}
bool MidiClient::isSuccess(int length){
if(length < 7){
//success\n is 8 letters
return false;
}
for(int i = 0; i < length && i < 7; i++){
string success="success";
if(this->buffer[i] == success[i]){
cout << success[i];
}
else{
cout << "\nfailing character: "<< buffer[i] <<endl;
return false;
}
}
cout <<endl;
return true; //looks like success to me
}