diff options
Diffstat (limited to 'Semestr 4')
26 files changed, 950 insertions, 0 deletions
diff --git a/Semestr 4/aisd/pracownia5/gen.py b/Semestr 4/aisd/pracownia5/gen.py new file mode 100644 index 0000000..283cd04 --- /dev/null +++ b/Semestr 4/aisd/pracownia5/gen.py @@ -0,0 +1,40 @@ +import random +import sys + +random.seed(sys.argv[1]) + +n = int(sys.argv[2]) +maxcoord = 1000 + + +print(n) +for i in range(n): + ty = random.randint(0, 3) + a, b = 0, 0 + y1, y2 = 0, 0 + if ty == 0: + a = random.randint(0, maxcoord) + b = random.randint(a+1, maxcoord + 1) + y1 = random.randint(0, maxcoord) + y2 = y1 + if ty == 1: + y1 = random.randint(0, maxcoord) + y2 = random.randint(a+1, maxcoord + 1) + a = random.randint(0, maxcoord) + b = a + if ty == 2: + a = random.randint(0, maxcoord) + b = random.randint(0, maxcoord) + x = random.randint(1, maxcoord) + y1 = b + b = a + x + y2 = y1 + x + if ty == 3: + a = random.randint(0, maxcoord) + b = random.randint(0, maxcoord) + x = random.randint(1, maxcoord) + y1 = b + b = a + x + y2 = y1 - x + + print(a, y1, b, y2) diff --git a/Semestr 4/aisd/pracownia5/wzo.cpp b/Semestr 4/aisd/pracownia5/wzo.cpp new file mode 100644 index 0000000..14056c8 --- /dev/null +++ b/Semestr 4/aisd/pracownia5/wzo.cpp @@ -0,0 +1,286 @@ +#include <bits/stdc++.h> +using namespace std; + +#define VER_T 1 +#define HOR_T 0 + +#define BEG 0 +#define END 1 + +struct event { + int x, y, z; + bool t; + event(int _x=0, bool _t=false, int _y=0, int _z=0) : x(_x), y(_y), z(_z), t(_t) {} + + bool operator < (const event &e) const { + if (x == e.x) { + if (t == e.t) return make_pair(y, z) < make_pair(e.y, e.z); + if (t == HOR_T) return z == BEG; + if (e.t == HOR_T) return e.z == END; + return t < e.t; + // if (t == HOR_T && z == BEG) return true; + // else if (t == HOR_T) return false; + // else if (e.t == HOR_T && e.z == BEG) return false; + // else if (e.t == HOR_T) return true; + // return (make_pair(y,z) < make_pair(e.y, e.z)); + } + return x < e.x; + // return make_pair(x, make_pair(t, make_pair(y, z))) < make_pair(e.x, make_pair(e.t, make_pair(e.y, e.z))); + } + +}; + +struct pnt { + int x, y; + + pnt(int _x=0, int _y=0) : x(_x), y(_y) {} + + void transpoze(int a11, int a12, int a21, int a22) { + int temp = x*a11 + y*a12; + y = x*a21 + y*a22; + x = temp; + } + bool operator == (const pnt &p) { + return (x==p.x && y == p.y); + } + +}; + +struct seg { + pnt st, nd; + + seg(pnt p1=pnt(), pnt p2=pnt()) : st(p1), nd(p2) {} + + void transpoze(int a11, int a12, int a21, int a22) { + st.transpoze(a11, a12, a21, a22); + nd.transpoze(a11, a12, a21, a22); + } +}; + +inline int sig(int x) { + if (x < 0) return -1; + if (x == 0) return 0; + if (x > 0) return 1; +} + +void swap(pnt &x, pnt &y) { + swap(x.x, y.x); + swap(x.y, y.y); +} + +vector<event> events; +map<int, int> points; + +void get_cross_pnts(vector<pnt> &result, vector<seg> &hor, vector<seg> &ver) { + events.resize(0); + points.clear(); + // cout << "h:\n"; + for (auto &s: hor) { + if (s.st.x > s.nd.x) swap(s.st, s.nd); + // cout << s.st.x << " " << s.st.y << ", " << s.nd.x << " " << s.nd.y << "\n"; + events.push_back(event(s.st.x, HOR_T, s.st.y, BEG)); + events.push_back(event(s.nd.x, HOR_T, s.st.y, END)); + } + // cout << "v:\n"; + for (auto &s: ver) { + if (s.st.y > s.nd.y) swap(s.st, s.nd); + // cout << s.st.x << " " << s.st.y << ", " << s.nd.x << " " << s.nd.y << "\n"; + + events.push_back(event(s.st.x, VER_T, s.st.y, s.nd.y)); + } + + sort(events.begin(), events.end()); + for (auto e: events) { + // cout << e.x << " " << (e.t == HOR_T ? "hor " : "ver ") << e.y << " " << (e.t == HOR_T ? (e.z == BEG ? "beg" : "end") : to_string(e.z)) << "\n"; + if (e.t == HOR_T) { + if (e.z == BEG) points[e.y]++; + else { + if (--points[e.y] == 0) points.erase(e.y); + } + } + else { + auto it = points.lower_bound(e.y); + while (it != points.end() && it->first <= e.z) { + result.push_back(pnt(e.x, it->first)); + it++; + } + } + } + // cout << "done\n"; +} + +vector<pnt> temp; +vector<seg> hor, ver, cross_left, cross_right; + +void read() { + int n; + // cin >> n; + scanf("%d", &n); + for (int i = 0; i < n; i++) { + int x1, y1, x2, y2; + // cin >> x1 >> y1 >> x2 >> y2; + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + seg s = seg(pnt(x1, y1), pnt(x2, y2)); + if (y1 == y2) + hor.push_back(s); + else if (x1 == x2) + ver.push_back(s); + else if (sig(x1 - x2) == sig(y1 - y2)) + cross_right.push_back(s); + else + cross_left.push_back(s); + } +} + +void hor_ver(vector<pnt> &result) { + // cout << "hor_ver\n"; + + temp.clear(); + get_cross_pnts(temp, hor, ver); + for (auto p: temp) result.push_back(p); +} + +void hor_c_r(vector<pnt> &result) { + // cout << "hor_cr\n"; + temp.clear(); + for (auto &p: hor) p.transpoze(1, -1, 0, 1); + for (auto &p: cross_right) p.transpoze(1, -1, 0, 1); + get_cross_pnts(temp, hor, cross_right); + for (auto &p: hor) p.transpoze(1, 1, 0, 1); + for (auto &p: cross_right) p.transpoze(1, 1, 0, 1); + + for (auto &p: temp) { + p.transpoze(1, 1, 0, 1); + result.push_back(p); + } +} + +void hor_c_l(vector<pnt> &result) { + // cout << "hor_cl\n"; + temp.clear(); + for (auto &p: hor) p.transpoze(1, 1, 0, 1); + for (auto &p: cross_left) p.transpoze(1, 1, 0, 1); + + get_cross_pnts(temp, hor, cross_left); + + for (auto &p: hor) p.transpoze(1, -1, 0, 1); + for (auto &p: cross_left) p.transpoze(1, -1, 0, 1); + + for (auto &p: temp) { + p.transpoze(1, -1, 0, 1); + result.push_back(p); + } +} + +void ver_c_r(vector<pnt> &result) { + // cout << "ver_cr\n"; + + temp.clear(); + for (auto &p: ver) { + p.transpoze(1, 0, -1, 1); + } + for (auto &p: cross_right) { + p.transpoze(1, 0, -1, 1); + } + + get_cross_pnts(temp, cross_right, ver); + + for (auto &p: ver) { + p.transpoze(1, 0, 1, 1); + } + for (auto &p: cross_right) { + p.transpoze(1, 0, 1, 1); + } + + for (auto &p: temp) { + p.transpoze(1, 0, 1, 1); + result.push_back(p); + } +} + +void ver_c_l(vector<pnt> &result) { + // cout << "ver_cl\n"; + + temp.clear(); + for (auto &p: ver) { + p.transpoze(-1, 0, 1, 1); + } + for (auto &p: cross_left) { + p.transpoze(-1, 0, 1, 1); + } + + get_cross_pnts(temp, cross_left, ver); + for (auto &p: ver) { + p.transpoze(-1, 0, 1, 1); + } + for (auto &p: cross_left) { + p.transpoze(-1, 0, 1, 1); + } + + for (auto &p: temp) { + p.transpoze(-1, 0, 1, 1); + result.push_back(p); + } +} + +void c_l_c_r(vector<pnt> &result) { + temp.clear(); + // cout << "cl_cr\n"; + for (auto &p: cross_right) { + // cout << p.st.x << " " << p.st.y << ", " << p.nd.x << " " << p.nd.y << "\n"; + p.transpoze(1, 1, -1, 1); + } + // cout << "---\n"; + for (auto &p: cross_left) { + // cout << p.st.x << " " << p.st.y << ", " << p.nd.x << " " << p.nd.y << "\n"; + p.transpoze(1, 1, -1, 1); + } + + get_cross_pnts(temp, cross_right, cross_left); + + for (auto &p: ver) { + p.transpoze(1, -1, 1, 1); + } + for (auto &p: cross_left) { + p.transpoze(1, -1, 1, 1); + } + + for (auto &p: temp) { + p.transpoze(1, -1, 1, 1); + result.push_back(p); + } +} + +void solve() { + // cout << hor.size() << " " << ver.size() << " " << cross_left.size() << " " << cross_right.size() << "\n"; + vector<pnt> result; + hor_ver(result); + hor_c_r(result); + hor_c_l(result); + ver_c_r(result); + ver_c_l(result); + for (auto &p: result) { + p.x *= 2; + p.y *= 2; + } + c_l_c_r(result); + + sort(result.begin(), result.end(), [&](const pnt &p1, const pnt &p2) { + if (p1.x == p2.x) return p1.y < p2.y; + return p1.x < p2.x; + }); + result.erase(unique(result.begin(), result.end()), result.end()); + for (auto p : result) { + printf("%d.%s ", p.x/2, (p.x % 2 == 1 ? "5" : "0")); + printf("%d.%s\n", p.y/2, (p.y % 2 == 1 ? "5" : "0")); + // cout << p.x/2 << "." << (p.x % 2 == 1 ? "5" : "0") << " "; + // cout << p.y/2 << "." << (p.y % 2 == 1 ? "5" : "0") << "\n"; + } +} + +int main() { + // ios_base::sync_with_stdio(false); + // cin.tie(); + read(); + solve(); +}
\ No newline at end of file diff --git a/Semestr 4/aisd/pracownia5/wzo2.cpp b/Semestr 4/aisd/pracownia5/wzo2.cpp new file mode 100644 index 0000000..b4e9ac3 --- /dev/null +++ b/Semestr 4/aisd/pracownia5/wzo2.cpp @@ -0,0 +1,184 @@ +#include <bits/stdc++.h> +using namespace std; + +/* +[0 -1] +[1 0] +*/ + +struct pnt { + int x, y; + pnt (int _x=0, int _y=0) : x(_x), y(_y) {} + void transpose(int a11, int a12, int a21, int a22) { + int temp = x*a11 + y*a12; + y = x*a21 + y*a22; + x = temp; + } + bool operator<(const pnt &p) const { + if (p.x == x) return y < p.y; + return x < p.x; + } + bool operator==(const pnt &p) const { + return (x == p.x && y == p.y); + } +}; + +struct seg { + pnt st, nd; + seg(pnt p1=pnt(), pnt p2=pnt()) : st(p1), nd(p2) {} + void transpose(int a11, int a12, int a21, int a22) { + st.transpose(a11, a12, a21, a22); + nd.transpose(a11, a12, a21, a22); + } +}; + +#define HOR_T 0 +#define VER_T 1 +#define CL_T 2 +#define CR_T 3 +#define BEG 0 +#define END 1 + +struct event { + int x; + int t; + int i1, i2; + + event(int _x, int _t, int _i1, int _i2) : x(_x), t(_t), i1(_i1), i2(_i2) {} + + bool operator < (const event &e) const { + if (x == e.x) { + if (t == e.t) return make_pair(i1, i2) < make_pair(e.i1, e.i2); + if (t == HOR_T) return (e.i2 == END); + if (e.t == HOR_T) return (i2 == BEG); + return t < e.t; + } + return x < e.x; + } +}; + +inline void swap(pnt &p1, pnt &p2) { + swap(p1.x, p2.x); + swap(p1.y, p2.y); +} + +vector<seg> segments; +vector<pnt> result; +vector<pnt> temp_res; +vector<event> events; +map<int, int> hor; +map<int, int> cl; +map<int, int> cr; + +void read() { + int n; + scanf("%d", &n); + segments.resize(n); + for (int i = 0; i < n; i++) { + cin >> segments[i].st.x >> segments[i].st.y >> segments[i].nd.x >> segments[i].nd.y; + } +} + +void rotate(int a11, int a12, int a21, int a22) { + for (auto s: segments) { + s.transpose(a11, a12, a21, a22); + } +} + +void solve_prob() { + temp_res.clear(); + events.clear(); + hor.clear(); cl.clear(); cr.clear(); + + for (auto s: segments) { + if (s.st.x > s.nd.x) swap(s.st, s.nd); + if (s.st.x == s.nd.x) { + if (s.st.y > s.nd.y) swap(s.st, s.nd); + events.push_back(event(s.st.x, VER_T, s.st.y, s.nd.y)); + } + else if (s.st.y == s.nd.y) { + events.push_back(event(s.st.x, HOR_T, s.st.y, BEG)); + events.push_back(event(s.nd.x, HOR_T, s.st.y, END)); + } + else if (s.st.y - s.nd.y < 0) { + events.push_back(event(s.st.x, CR_T, s.st.y, BEG)); + events.push_back(event(s.nd.x, CR_T, s.nd.y, END)); + } + else { + events.push_back(event(s.st.x, CL_T, s.st.y, BEG)); + events.push_back(event(s.nd.x, CL_T, s.nd.y, END)); + } + } + + sort(events.begin(), events.end()); + for (auto e: events) { + if (e.t == VER_T) { + auto it = hor.lower_bound(e.i1); + while (it != hor.end() && it->first <= e.i2) { + temp_res.push_back({e.x, it->first}); + it++; + } + it = cr.lower_bound(e.i1 - e.x); + while (it != cr.end() && it->first <= e.i2 - e.x) { + temp_res.push_back({e.x, it->first + e.x}); + it++; + } + it = cl.lower_bound(e.i1 + e.x); + while (it != cl.end() && it->first <= e.i2 + e.x) { + temp_res.push_back({e.x, it->first - e.x}); + it++; + } + } + if (e.t == CR_T) { + if (e.i2 == BEG) cr[e.i1 - e.x]++; + else if (--cr[e.i1 - e.x] <= 0) cr.erase(e.i1 - e.x); + } + if (e.t == CL_T) { + if (e.i2 == BEG) cl[e.i1 + e.x]++; + else if (--cl[e.i1 + e.x] <= 0) cl.erase(e.i1); + } + if (e.t == HOR_T) { + if (e.i2 == BEG) hor[e.i1]++; + else if (--hor[e.i1] <= 0) hor.erase(e.i1); + } + } +} + +void add_to_res(int a11, int a12, int a21, int a22) { + for (auto p: temp_res) { + p.transpose(a11, a12, a21, a22); + result.push_back(p); + } +} + +void solve() { + solve_prob(); + for (auto &p: temp_res) { + p.x *= 2; p.y *= 2; + } + add_to_res(1, 0, 0, 1); + for (auto &p: temp_res) { + p.x /= 2; p.y /= 2; + } + rotate(1, -1, 1, 1); + solve_prob(); + add_to_res(1, 1, -1, 1); + rotate(1, 1, -1, 1); + for (auto &p: temp_res) { + p.x /= 2; p.y /= 2; + } + + + + sort(result.begin(), result.end()); + result.erase(unique(result.begin(), result.end()), result.end()); + for (auto p : result) { + cout << p.x/2 << "." << (p.x % 2 == 1 ? "5" : "0") << " "; + cout << p.y/2 << "." << (p.y % 2 == 1 ? "5" : "0") << "\n"; + } +} + +int main() { + read(); + solve(); +}
\ No newline at end of file diff --git a/Semestr 4/aisd/pracownia6/wzo.cpp b/Semestr 4/aisd/pracownia6/wzo.cpp new file mode 100644 index 0000000..f13f0ec --- /dev/null +++ b/Semestr 4/aisd/pracownia6/wzo.cpp @@ -0,0 +1,51 @@ +#include <bits/stdc++.h> +using namespace std; + +const int N = 1e6 + 10; +bool vis[N]; +int n, m, ranga[N], par[N]; +pair<int, pair<int, int>> G[N]; + +int Find(int v) { + if (par[v] == v) return v; + return par[v] = Find(par[v]); +} + +void Union(int v, int u) { + if (ranga[v] > ranga[u]) { + par[u] = v; + } + else { + if (ranga[v] == ranga[u]) ranga[u]++; + par[v] = u; + } +} + +bool traj(int maks) { + +} + +int main() { + scanf("%d%d", &n, &m); + for (int i = 0; i < m; i++) { + int a, b, w; + scanf("%d%d%d", &a, &b, &w); + G[i] = {-w, {a,b}}; + } + sort(G, G+m); + for (int i = 1; i <= n; i++) { + par[i] = i; + ranga[i] = 0; + } + int mini = 1e9; + for (int i = 0; i < m; i++) { + int a = G[i].second.first; + int b = G[i].second.second; + a = Find(a); b = Find(b); + if (a != b) { + Union(a, b); + mini = min(mini, -G[i].first); + } + } + printf("%d\n", mini); +}
\ No newline at end of file diff --git a/Semestr 4/ask/lista11/gen.py b/Semestr 4/ask/lista11/gen.py new file mode 100644 index 0000000..d4de762 --- /dev/null +++ b/Semestr 4/ask/lista11/gen.py @@ -0,0 +1,65 @@ +def next_permutation(a): + """Generate the lexicographically next permutation inplace. + + https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order + Return false if there is no next permutation. + """ + # Find the largest index i such that a[i] < a[i + 1]. If no such + # index exists, the permutation is the last permutation + for i in reversed(range(len(a) - 1)): + if a[i] < a[i + 1]: + break # found + else: # no break: not found + return False # no next permutation + + # Find the largest index j greater than i such that a[i] < a[j] + j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j]) + + # Swap the value of a[i] with that of a[j] + a[i], a[j] = a[j], a[i] + + # Reverse sequence from a[i + 1] up to and including the final element a[n] + a[i + 1:] = reversed(a[i + 1:]) + return True + +def perm_to_str(a): + return ''.join(map(str, a)) + +def str_to_perm(s): + return [int(c) for c in s] + +t = [0,1,2,3] +perm_to_idx = dict() + +def bbin(x): + return bin(x)[2:] + +cnt = 23 +while True: + perm_to_idx[perm_to_str(t)] = cnt + cnt -= 1 + if not next_permutation(t): + break + +for p in perm_to_idx.keys(): + for i in range(4): + t = str_to_perm(p) + for j in range(4): + if t[j] > t[i]: + t[j] -= 1 + t[i] = 3 + print(p, 'x', i, '->', perm_to_str(t), ':\t', bbin(perm_to_idx[p]), '\t', bbin(i), '\t', bbin(perm_to_idx[perm_to_str(t)]), end='\t') + print(perm_to_idx[p], '\t', i, '\t', perm_to_idx[perm_to_str(t)]) + +print('\n-----\n') + + +for p in perm_to_idx.keys(): + for i in range(4): + t = str_to_perm(p) + for j in range(4): + if t[j] > t[i]: + t[j] -= 1 + t[i] = 3 + print(p, 'x', i, '->', perm_to_str(t), ':\t', bbin(perm_to_idx[p]), '\t', bbin(i), '\t', bbin(perm_to_idx[perm_to_str(t)]), end='\t') + print(perm_to_idx[p], '\t', i, '\t', perm_to_idx[perm_to_str(t)]) diff --git a/Semestr 4/ask/lista11/zad8.c b/Semestr 4/ask/lista11/zad8.c new file mode 100644 index 0000000..cd8c2bf --- /dev/null +++ b/Semestr 4/ask/lista11/zad8.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdint.h> + +uint8_t victim(uint8_t s) { + s |= ((s & 0x55) << 1) | (s & 0xaa >> 1); + return ((s >> 2) && 1) | ((s >> 4) && 2) | ((s >> 6) && 3); +} + +uint8_t update(uint8_t s, uint8_t v) { + int8_t p0 = victim(s); + int8_t p1 = victim(s ^ 0b01010101); + int8_t p2 = victim(s ^ 0b10101010); + int8_t p3 = victim(s ^ 0b11111111); + uint8_t age = (3 << (v << 1)) & s; + + s -= (~((char)(age - 1) >> 7)) & (1 << (p1 << 1)); + s -= (~((char)(age - 2) >> 7)) & (1 << (p2 << 1)); + s -= (~((char)(age - 3) >> 7)) & (1 << (p3 << 1)); + s |= (3 << (v << 1)); + return s; +} + +int main() {}
\ No newline at end of file diff --git a/Semestr 4/pstwo/lista10/L10.pdf b/Semestr 4/pstwo/lista10/L10.pdf Binary files differnew file mode 100644 index 0000000..abe2ff9 --- /dev/null +++ b/Semestr 4/pstwo/lista10/L10.pdf diff --git a/Semestr 4/pstwo/lista10/RP1R_10.pdf b/Semestr 4/pstwo/lista10/RP1R_10.pdf Binary files differnew file mode 100644 index 0000000..26374ca --- /dev/null +++ b/Semestr 4/pstwo/lista10/RP1R_10.pdf diff --git a/Semestr 4/pstwo/lista7/RP1R_07.pdf b/Semestr 4/pstwo/lista7/RP1R_07.pdf Binary files differnew file mode 100644 index 0000000..f159bf0 --- /dev/null +++ b/Semestr 4/pstwo/lista7/RP1R_07.pdf diff --git a/Semestr 4/pstwo/lista8/RP1R_08.pdf b/Semestr 4/pstwo/lista8/RP1R_08.pdf Binary files differnew file mode 100644 index 0000000..6a5140e --- /dev/null +++ b/Semestr 4/pstwo/lista8/RP1R_08.pdf diff --git a/Semestr 4/pstwo/lista9/RP1R_09.pdf b/Semestr 4/pstwo/lista9/RP1R_09.pdf Binary files differnew file mode 100644 index 0000000..5baaf9f --- /dev/null +++ b/Semestr 4/pstwo/lista9/RP1R_09.pdf diff --git a/Semestr 4/pstwo/lista9/l9z3.pdf b/Semestr 4/pstwo/lista9/l9z3.pdf Binary files differnew file mode 100644 index 0000000..784523f --- /dev/null +++ b/Semestr 4/pstwo/lista9/l9z3.pdf diff --git a/Semestr 4/pstwo/wyklady/Wyklad 10.pdf b/Semestr 4/pstwo/wyklady/Wyklad 10.pdf Binary files differnew file mode 100644 index 0000000..1224e18 --- /dev/null +++ b/Semestr 4/pstwo/wyklady/Wyklad 10.pdf diff --git a/Semestr 4/pstwo/wyklady/Wyklad 11.pdf b/Semestr 4/pstwo/wyklady/Wyklad 11.pdf Binary files differnew file mode 100644 index 0000000..c5611dd --- /dev/null +++ b/Semestr 4/pstwo/wyklady/Wyklad 11.pdf diff --git a/Semestr 4/pstwo/wyklady/Wyklad 12.pdf b/Semestr 4/pstwo/wyklady/Wyklad 12.pdf Binary files differnew file mode 100644 index 0000000..a7572be --- /dev/null +++ b/Semestr 4/pstwo/wyklady/Wyklad 12.pdf diff --git a/Semestr 4/sieci/cwiczenia2/c2.pdf b/Semestr 4/sieci/cwiczenia2/c2.pdf Binary files differnew file mode 100644 index 0000000..3216f6f --- /dev/null +++ b/Semestr 4/sieci/cwiczenia2/c2.pdf diff --git a/Semestr 4/sieci/pracownia3/config.h b/Semestr 4/sieci/pracownia3/config.h new file mode 100644 index 0000000..3b81646 --- /dev/null +++ b/Semestr 4/sieci/pracownia3/config.h @@ -0,0 +1,10 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define DATAGRAM_LEN 1000 +#define HEADER_LEN 40 + +#define WINDOW_SIZE 3000 +#define TIMEOUT 100 + +#endif diff --git a/Semestr 4/sieci/pracownia3/transport.c b/Semestr 4/sieci/pracownia3/transport.c new file mode 100644 index 0000000..9226d69 --- /dev/null +++ b/Semestr 4/sieci/pracownia3/transport.c @@ -0,0 +1,149 @@ +/* Projekt: Transport + * Autor: Franciszek Malinka 316093 + */ + +#include <netinet/ip.h> +#include <arpa/inet.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <stdbool.h> +#include "config.h" +#include "window.h" +#include "utils.h" + +size_t send_datagram(int sockfd, struct sockaddr_in server_address, char *buffer, size_t buffer_len) { + return sendto(sockfd, buffer, buffer_len, 0, (struct sockaddr*) &server_address, sizeof(server_address)); +} + +void send_data_request(int sockfd, struct sockaddr_in server_address, size_t pos, size_t bytes) { + char buffer[40]; + sprintf(buffer, "GET %ld %ld\n", pos, bytes); + size_t buffer_len = strlen(buffer); + if (send_datagram(sockfd, server_address, buffer, buffer_len) != buffer_len) { + fprintf(stderr, "sendto error: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } +} + +size_t recv_message(int sockfd, char *buffer, struct sockaddr_in *sender) { + socklen_t sender_len = sizeof(*sender); + bzero(buffer, HEADER_LEN + DATAGRAM_LEN); + bzero(sender, sizeof(*sender)); + size_t datagram_len = recvfrom(sockfd, buffer, IP_MAXPACKET, 0, + (struct sockaddr*)sender, &sender_len); + + if (datagram_len < 0) { + fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + return datagram_len; +} + +void request_data(int sockfd, struct sockaddr_in server_address, window_t *w, size_t bytes_writen, size_t remaining_bytes) { + int pos = w->first_pos; + for (int i = 0; i < w->size && i*DATAGRAM_LEN < remaining_bytes; i++) { + if (w->uptodate[pos] == false) { + size_t bytes_to_request = min(DATAGRAM_LEN, remaining_bytes - i*DATAGRAM_LEN); + send_data_request(sockfd, server_address, bytes_writen + i*DATAGRAM_LEN, bytes_to_request); + } + pos = (pos + 1) % w->size; + } +} + +void update_file(FILE *fd, window_t *w, size_t *bytes_writen, size_t *remaining_bytes) { + while (w->uptodate[w->first_pos] && *remaining_bytes > 0) { + // printf("Writing %ld\n", *bytes_writen); + size_t bytes_to_write = min(DATAGRAM_LEN, *remaining_bytes); + fwrite(w->ar[w->first_pos], sizeof(char), bytes_to_write, fd); + *bytes_writen += bytes_to_write; + *remaining_bytes -= bytes_to_write; + shift(w); + } +} + +size_t recv_datagram(int sockfd, char *buffer, struct sockaddr_in server_address) { + struct sockaddr_in sender; + + size_t received_bytes = recv_message(sockfd, buffer, &sender); + if (sender.sin_addr.s_addr != server_address.sin_addr.s_addr || sender.sin_port != server_address.sin_port) { + printf("Smieci!\n"); + return 0; + } + return received_bytes; +} + + +void receive_file(int sockfd, struct sockaddr_in server_address, const char *file_name, size_t remaining_bytes) { + FILE *fd = fopen(file_name, "w"); + if (!fd) { + fprintf(stderr, "fopen error: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + size_t bytes_writen = 0; + + size_t recv_pos, recv_len; + char buffer[DATAGRAM_LEN + HEADER_LEN]; + int prev_len = 0; + window_t w; + init_window(&w, WINDOW_SIZE, DATAGRAM_LEN); + + while (remaining_bytes) { + request_data(sockfd, server_address, &w, bytes_writen, remaining_bytes); + int timeout = TIMEOUT; + while (poll_socket_modify_timeout(sockfd, &timeout)) { + size_t received_bytes = recv_datagram(sockfd, buffer, server_address); + if (received_bytes == 0) continue; + sscanf(buffer, "DATA %ld %ld\n", &recv_pos, &recv_len); + if (recv_pos < bytes_writen) continue; + + int pos = (recv_pos - bytes_writen) / DATAGRAM_LEN; + pos = (pos + w.first_pos) % w.size; + if (!w.uptodate[pos]) { + for (int i = 0; i < recv_len; i++) { + w.ar[pos][i] = buffer[i + received_bytes - recv_len]; + } + w.uptodate[pos] = true; + } + update_file(fd, &w, &bytes_writen, &remaining_bytes); + } + + if (prev_len != bytes_writen) { + prev_len = bytes_writen; + printf("%.3f%%\n", 100.0 * (float)(bytes_writen) / (float)(remaining_bytes+bytes_writen)); + } + } + destroy_window(&w); + fclose(fd); +} + +int main(int argc, char *argv[]) { + if (argc != 5) { + printf("Usage:\n\t%s [server ip] [server port] [output file name] [file size]\n", argv[0]); + return -1; + } + + int sockfd = get_socket(); + struct sockaddr_in server_address; + bzero(&server_address, sizeof(server_address)); + server_address.sin_family = AF_INET; + if (!inet_pton(AF_INET, argv[1], &server_address.sin_addr)) { + fprintf(stderr, "Invalid ip address: %s\n", argv[1]); + return -1; + } + server_address.sin_port = htons(atoi(argv[2])); + if (server_address.sin_port == 0) { + fprintf(stderr, "Invalid port: %s\n", argv[2]); + return -1; + } + + size_t file_len = atoi(argv[4]); + if (file_len == 0) { + printf("File len is 0, nothing to do here.\n"); + return 0; + } + + receive_file(sockfd, server_address, argv[3], file_len); +}
\ No newline at end of file diff --git a/Semestr 4/sieci/pracownia3/utils.c b/Semestr 4/sieci/pracownia3/utils.c new file mode 100644 index 0000000..4c2f449 --- /dev/null +++ b/Semestr 4/sieci/pracownia3/utils.c @@ -0,0 +1,58 @@ +/* Projekt: Transport + * Autor: Franciszek Malinka 316093 + */ + +#include "utils.h" +#include <poll.h> +#include <errno.h> +#include <stdio.h> +#include <netinet/ip.h> +#include <arpa/inet.h> +#include <stdlib.h> +#include <string.h> + +size_t min(size_t x, size_t y) { return (x<y ? x : y); } + +int get_socket() { + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket error: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + return sockfd; +} + +long get_time_interval(struct timespec start, struct timespec finish) { + return S_TO_MS(finish.tv_sec - start.tv_sec) + NS_TO_MS(finish.tv_nsec - start.tv_nsec); +} + +int poll_socket_modify_timeout(int sockfd, int *timeout) { + if (*timeout < 0) { + *timeout = 0; + return 0; + } + + struct pollfd fds; + struct timespec start; + struct timespec finish; + + fds.fd = sockfd; + fds.events = POLLIN; + fds.revents = 0; + + clock_gettime(CLOCK_REALTIME, &start); + int result = poll(&fds, 1, *timeout); + clock_gettime(CLOCK_REALTIME, &finish); + + if (result == -1) { + fprintf(stderr, "poll error: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + if (result == 0) { + *timeout = 0; + return 0; + } + + *timeout -= get_time_interval(start, finish); + return result; +} diff --git a/Semestr 4/sieci/pracownia3/utils.h b/Semestr 4/sieci/pracownia3/utils.h new file mode 100644 index 0000000..eccf347 --- /dev/null +++ b/Semestr 4/sieci/pracownia3/utils.h @@ -0,0 +1,18 @@ +#ifndef UTILS_H +#define UTILS_H + +#include <stddef.h> +#include <time.h> + +#define NS_TO_MS(X) ((long)(X) / (long)1000000) +#define S_TO_MS(X) ((long)(X) * (long)1000) + +size_t min(size_t x, size_t y); + +int get_socket(); + +long get_time_interval(struct timespec start, struct timespec finish); + +int poll_socket_modify_timeout(int sockfd, int *timeout); + +#endif
\ No newline at end of file diff --git a/Semestr 4/sieci/pracownia3/window.c b/Semestr 4/sieci/pracownia3/window.c new file mode 100644 index 0000000..c8bffad --- /dev/null +++ b/Semestr 4/sieci/pracownia3/window.c @@ -0,0 +1,42 @@ +/* Projekt: Transport + * Autor: Franciszek Malinka 316093 + */ + +#include "window.h" +#include <stdlib.h> +#include <strings.h> + +void init_window(window_t *w, int window_size, int window_width) { + w->ar = malloc(window_size * sizeof(char *)); + for (int i = 0; i < window_size; i++) + w->ar[i] = malloc(window_width * sizeof(char)); + w->uptodate = malloc(window_size); + bzero (w->uptodate, window_size); + w->first_pos = 0; + w->size = window_size; +} + +void destroy_window(window_t *w) { + for (int i = 0; i < w->size; i++) + free(w->ar[i]); + free(w->ar); + free(w->uptodate); +} + +void shift_while_uptodate(window_t *w) { + while (w->uptodate[w->first_pos]) { + w->uptodate[w->first_pos] = false; + w->first_pos = (w->first_pos + 1) % w->size; + } +} + +void shift(window_t *w) { + w->uptodate[w->first_pos] = false; + w->first_pos = (w->first_pos + 1) % w->size; +} + +void update(window_t *w, int pos, char *buffer, size_t buf_size) { + pos = (w->first_pos + pos) % w->size; + for (int i = 0; i < buf_size; i++) w->ar[pos][i] = buffer[i]; + w->uptodate[pos] = true; +}
\ No newline at end of file diff --git a/Semestr 4/sieci/pracownia3/window.h b/Semestr 4/sieci/pracownia3/window.h new file mode 100644 index 0000000..63e89cc --- /dev/null +++ b/Semestr 4/sieci/pracownia3/window.h @@ -0,0 +1,24 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include <stdbool.h> +#include <stddef.h> + +typedef struct { + char **ar; + bool *uptodate; + int size; + int first_pos; +} window_t; + +void init_window(window_t *w, int window_size, int window_width); + +void destroy_window(window_t *w); + +void shift_while_uptodate(window_t *w); + +void shift(window_t *w); + +void update(window_t *w, int pos, char *buffer, size_t buf_size); + +#endif
\ No newline at end of file diff --git a/Semestr 4/sieci/pracownia4/p4.pdf b/Semestr 4/sieci/pracownia4/p4.pdf Binary files differnew file mode 100644 index 0000000..bdaa771 --- /dev/null +++ b/Semestr 4/sieci/pracownia4/p4.pdf diff --git a/Semestr 4/sieci/warsztaty6/w6.pdf b/Semestr 4/sieci/warsztaty6/w6.pdf Binary files differnew file mode 100644 index 0000000..2f6b6c9 --- /dev/null +++ b/Semestr 4/sieci/warsztaty6/w6.pdf diff --git a/Semestr 4/sieci/wyklady/lec7.pdf b/Semestr 4/sieci/wyklady/lec7.pdf Binary files differnew file mode 100644 index 0000000..0b28c4d --- /dev/null +++ b/Semestr 4/sieci/wyklady/lec7.pdf diff --git a/Semestr 4/sieci/wyklady/lec8.pdf b/Semestr 4/sieci/wyklady/lec8.pdf Binary files differnew file mode 100644 index 0000000..a4fbb9c --- /dev/null +++ b/Semestr 4/sieci/wyklady/lec8.pdf |