Recent Entries 10
- pattern minor 112d agoWebcam frame splitterI wrote a camera streaming app for pizero. Since the pizero is too weak to do any video encoding I am using it to just capture usb webcam frames (mjpeg format) and forward them over udp to a powerful pc that does the video encoding. When the light conditions are low there is a lot of noise introduced to the frames and they grow in size, overflowing the udp write size limit. To tackle the issue I wrote a func that splits the frame in parts and forwards the pieces to the encoder. What I am looking for in this review is the following: Performance - App runs on pizero where resources are limited. I am using buffered channels and I am not sure if I implemented them correctly. Best practices Hidden pitfalls - If any, point out if I shot myself in the foot. Code contains the whole camera implementation plus the server side. Please review both. My programming level is beginner. This is my first project. Camera: ``` package main import ( "bytes" "fmt" "log" "math" "net" "os/exec" "sync" "time" "labix.org/v2/mgo/bson" ) const ( FFMPEG_ENCODER = "192.168.178.200:8000" CAM_ADDR = ":5000" PACKET = 60000 ) type msg struct { Fragment bool FragmentID int LastFragment bool Data []byte } type reporting struct { Fps int Size int Fragments float64 Encoding time.Duration Writing time.Duration } func main() { conn, err := udpDial() if err != nil { log.Fatal(err) } var stderr bytes.Buffer //bash script with commands for interfacing with the camera. //script outputs to stdout. cmd := exec.Command("./v4l2") pipe, _ := cmd.StdoutPipe() defer pipe.Close() cmd.Stderr = &stderr if err := cmd.Start(); err != nil { fmt.Println(fmt.Sprint(err) + ": " + stderr.String()) } //pizero - has issues serializing. buffer := make(chan [][]byte, 500) var wg sync.WaitGroup var sta
- pattern minor 112d agoSimple Rust WebserverI started learning Rust today and for my first project created a simple webserver. I don't have a clear vision about Rust programming and I tried to write it with my previous knowledge in Python and PHP. ``` use std::io::{Read, Write,BufReader, BufRead}; use std::net::{TcpListener, TcpStream}; use std::fs::File; use std::path::Path; fn main(){ loop{ let listener = TcpListener::bind("localhost:8000").unwrap(); let stream = listener.accept().unwrap().0; read_request(stream); } } fn read_request(stream: TcpStream){ let mut lines = String::new(); let mut reader = BufReader::new(stream); reader.read_line(&mut lines); let mut vec_line = lines.split_whitespace(); let mut requested_page = vec_line.nth(1).unwrap().to_string(); requested_page = requested_page.replace("/",""); if requested_page == "" { requested_page = String::from("index.html"); } let mut response = String::new(); let path = Path::new(&requested_page); println!("{}",requested_page); let mut status = 200; if !path.exists(){ response = String::from("Not Found!"); status = 404; } else { let mut file = File::open(&requested_page).expect("Unable to open file"); file.read_to_string(&mut response); } send_response(reader.into_inner(), &response.to_string(), status); } fn send_response(mut stream: TcpStream, res :&str, status :u32){ let response = format!("{}{}{}{}","HTTP/1.1 ",status," OK\n\n",res); stream.write_all(response.as_bytes()).unwrap(); } ```
- pattern minor 112d agoBeginnings of Go web server with mongoDbI am just learning how to use go and I'm a bit confused on how my code is supposed to be organised package wise. Am I right to have the `user` package? I'm planning to redirect my `/user/` routes to a handler in this package but I'm unsure if this is the `go` way to do things My directory structure is ``` /go_rest_api /user -user.go -user_provider.go -app.go -main.go ``` main.go ``` package main func main() { a := App{} a.Initialize() a.Run() } ``` app.go ``` package main import ( "fmt" "log" "github.com/gorilla/mux" "gopkg.in/mgo.v2" "go_rest_api/user" ) type App struct { Router *mux.Router Mongo *MongoConnection } type MongoConnection struct { Session *mgo.Session } func(a* App) GetMongoSession() *mgo.Session { return a.Mongo.Session.Copy() } func(a *App) Initialize() { session, err := mgo.Dial("127.0.0.1:27017") if err != nil { panic(err) } session.SetMode(mgo.Monotonic, true) a.Mongo = &MongoConnection{session} a.Router = mux.NewRouter() } func(a *App) Run() { defer a.Mongo.Session.Close() testMongo(a.GetMongoSession()) } func testMongo(session *mgo.Session) { userProvider := user.Provider(session) err := userProvider.InsertUser(user.User{"test"}) if err != nil { log.Fatal(err) } user := user.User{} err, user = userProvider.GetUser("test") if err != nil { log.Fatal(err) } fmt.Println("username:", user.Username) } ``` user/user.go ``` package user type User struct { Username string } ``` user/user_provider.go ``` package user import ( "gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2" ) type provider struct { Collection *mgo.Collection } func Provider(mongoSession *mgo.Session) provider { p := provider{} p.Collection = mongoSession.DB("test").C("user") return p } func(p *provider) InsertUser(user User) error { return p.Collection.Insert(&user) } func (p *provider) GetUser(username string) (error, User) { result := User{} err := p.Collection.
- pattern minor 112d agoServer-client data transferI coded a server-client (kind of) chat, and I need your review as I'm sure it is a mess. I used lots of tutorials and tips form different websites and forums which were posted at different times (maybe now it's a lot easier to write what I want) and slapped them on Visual Studio. It's not like I wanted it to be a chat, but a way to communicate with a server as a client. Server-Side ``` using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace First_Server { class Program { private static void SocketThread(object obj) { var ClientSocket = (TcpClient)obj; while (true) { try { NetworkStream NetworkStream = ClientSocket.GetStream(); byte[] BytesFromClient = new byte[10025]; NetworkStream.Read(BytesFromClient, 0, ClientSocket.ReceiveBufferSize); string DataFromClient = Encoding.ASCII.GetString(BytesFromClient); DataFromClient = DataFromClient.Substring(0, DataFromClient.IndexOf("#")); Console.WriteLine(DataFromClient); string ServerResponse = DataFromClient; Byte[] SendBytes = Encoding.ASCII.GetBytes(ServerResponse); NetworkStream.Write(SendBytes, 0, SendBytes.Length); } catch (Exception StreamException) { Console.WriteLine("Connection from a client closed."); ClientSocket.Close(); } } } static void Main(string[] args) { int Port = 42069; IPAddress LocalIPAddress = IPAddress.Parse("192.168.1.10"); TcpListener ServerSocket = new TcpListener(LocalIPAddress, Port); TcpClient ClientSocket; ServerSocket.Start(); Console.WriteLine("Server started. Waiting for requests..."); while (true) { ClientSocket = ServerSocket.AcceptTcpClient(); Console.WriteLi
- pattern minor 112d agoStrip a newline and add more text to a Go bytes.BufferI wrote a little timer middleware to append the request duration to the end of the log message returned by the excellent Gorilla Toolkit's `CombinedLoggingHandler` from the `handlers` package. That handler accepts an `io.Writer` and an `http.Handler`, and fills the `Writer` with a newline-terminate string containing the Apache Combined Format log entry for the current server request. I would like to remove the trailing newline and add `" completed in xxx"`, where `xxx` is the amount of time the request took. The following code works, but I am not sure that I made the best use of the Go `bytes.Buffer` in the way I stripped the newline: ``` func timerHandler(h http.Handler) http.Handler { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { var b bytes.Buffer hand := handlers.CombinedLoggingHandler(&b, h) defer func(start time.Time) { prefix := bytes.TrimRight(b.Bytes(), "\n") os.Stdout.Write(prefix) fmt.Printf(" completed in %v\n", time.Since(start)) }(time.Now()) hand.ServeHTTP(res, req) }) } ``` Is there a better way to write this in Go?
- pattern minor 112d agoServer side of a chat programThis is just the server part of a chat program. The user of the server can receive and send messages to all the clients connected to the server. Everything works fine but since I'm a beginner I don't know if I should avoid something or not, what I should improve, etc.. The server class of the app: ``` import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; public class ServerSide { private static ServerSocket ss; private static Socket sock; private int port, ID; private static String textReceived; private static Map clientList = new HashMap(); public ServerSide() { } // Main Constructor takes the port to connect to a server // in order to chat with the clients public ServerSide(int port) throws IOException { this.port = port; ss = new ServerSocket(port); Thread t1 = new Thread(new Runnable() { @Override public void run() { while (true) { try { sock = ss.accept(); //Every client has an unique ID that will be use to send the receiving messages //to all the clients except to the sender ID++; clientList.put(new Client(sock, ID), ID); } catch (IOException e) { e.printStackTrace(); } } } }); t1.start(); } public Map getClientsList() { return clientList; } //This method is used everytime the user of the server clicks on "Send". protected void send(String textSent) throws IOException { new SendMessages(clientList, textSent); } //Return the text to append on the TextArea of the chat. public String getTextReceived() { return textReceived; } public void setTextReceived(String textReceived) { this.textReceived = textReceived; } } class SendMessages
- pattern minor 112d agoReading properties from file during standalone application startupI've a standalone Java application, which basically starts and manages two socket servers. I'd like to configure server ports in a `.properties` file using the following class. ``` class ApplicationConfig { private static final Logger LOG = Logger.getLogger(ApplicationConfig.class.getName()); private static final Properties APP_PROPERTIES = new Properties(); static int defaultEventServerPort = 9090; static int defaultClientServerPort = 9099; static { try { APP_PROPERTIES.load(ClassLoader.class.getResourceAsStream("/app.properties")); String eventServerPort = APP_PROPERTIES.getProperty("server.event.port"); String clientServerPort = APP_PROPERTIES.getProperty("server.client.port"); if (isValidNumeric(eventServerPort)) { defaultEventServerPort = Integer.valueOf(eventServerPort); } if (isValidNumeric(clientServerPort)) { defaultClientServerPort = Integer.valueOf(clientServerPort); } } catch (IOException ex) { LOG.log( Level.WARNING, "Unable to load server ports from properties file, going to use default port {0} for event server and {1} for client server", new Object[]{defaultEventServerPort, defaultClientServerPort} ); } } private static boolean isValidNumeric(String v) { if (v == null || v.length() == 0) { return false; } for (int i = 0; i < v.length(); i++) { if (!Character.isDigit(v.charAt(i))) { return false; } } return true; } } ``` I really hate `ApplicationConfig` class, the static initialization block bothers me, but I am not able to find a better idea yet. How you would suggest to modify it? And here is my `main` class ``` public class Application { private static final Logger LOG = Logger.getLogge
- pattern minor 112d agoSimple file server for GET requestsI recently made this simple server in C for Linux systems and was wanting to get another set of eyes on it for a review of the design. I am new to socket programming and used a textbook from school to guide my way. It is for GET requests from browsers and serves from a public folder (or other destination of choice). It seems to work with all the files I have thrown at it so far. If you test it out, keep in mind that it only works with files that have their type extensions. For example, "index.html" would have to be requested as "index.html" in the browser. I was wanting information about how sessions and POST requests also work on professional servers Net.h ``` #ifndef NET_H_INCLUDED #define NET_H_INCLUDED #include #include #include #include #include #include #include #include #include #include #include #include #include #include /** * common functions for TCP IP4 server * ********************************************************************/ typedef struct sockaddr_in SA_in; typedef struct sockaddr SA; // configure server address int configure_server_IP4(struct sockaddr_in *, uint16_t, int); int sanitary(const char *); int compareFileType(const char *, const char *); int sendPage(const char *, int); typedef struct { int fd_client; char *file_name; } GetRequest; GetRequest * prepareGetRequest(int, const char *, const char *); void * handleGetRequest(void *); #endif // NET_H_INCLUDED ``` Net.c ``` #include "Net.h" const char *HEADER = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "\r\n"; const char *FILE_NOT_FOUND = "\r\n" "\r\n" "\r\n" " 404 Error\r\n" "\r\n" "\r\n" " 404 Error: File Not Found\r\n" "\r\n"
- snippet minor 112d agoNode js server that create, read, and update user data using TCP socket and AES encryptionI've written a Node js server that create, read, and update user data from a SQL server hosted in Azure. Clients connect to the server using a standard TCP socket, every message sent or received will be encrypted/decrypted using AES 256 CBC. Communication protocol TCP is used to create a simple communication mean without the over head of HTTP/HTTPS, and the client will try to persist that connection in order to eliminate the need to connect every time a message is sent. That will also give us the ability to know the number of connected players, and to be able to send messages from the server to all connected players. Authentication There are no authentication at the moment, anyone could connect to the server using telnet or any TCP based client. However any data sent will be decrypted if decryption or JSON parsing failed the connection will be closed. I am open for any suggestion in this area. Message format All messages exchanged between the server and client uses JSON format. Messages sent from client to server are formatted as the following JSON message: ``` { "ID": 0 // a number indicating which function to call "msg": "{ // embedded JSON contains the data ..... }" } ``` Messages sent from server to client - Number indicating if the operation was successful(1) or not(0). Or - JSON containing data about the player, if the operation was about fetching player data. Message process All JSON messages received are parsed asynchronously and messages sent to the client are stringifyied asynchronously to avoid Nodejs main loop blocking. Server.js ``` /* Library to add date and time to any console.log */ require('log-timestamp'); /* Asynchronous JSON parse library */ var parseJSON = require('json-parse-async'); /* Asynchronous JSON stringify library */ var asyncJSON = require('async-json'); /* Keymetrics advance metrics */ var pmx = require('pmx').init({ http: false, /
- pattern minor 112d agoNode.js API Server for querying a Neo4j databaseI've written a server in Node.js that listens to http requests via Express.js and forwards requests to a Neo4j server. It's my first time working with Neo4j and Express.js. The code runs as intended but is an eyesore to read. The Database has the following Nodes and Relationships: ``` (:User)-[:RANTED]->(:Rant) (:User)-[:UPVOTED]->(:Rant) (:User)-[:DOWNVOTED]->(:Rant) (:Rant)-[:HAS_COMMENT]->(:Comment) (:User)-[:COMMENTED]->(:Comment) (:User)-[:UPVOTED]->(:Comment) (:User)-[:DOWNVOTED]->(:Comment) ``` And the API simply supports creation of these nodes and relationships. Edit and Delete functionality is out of the project's scope. NOTE: The server is meant for a simplistic social network for my final year university project. We are only being marked on the deliverables and not the code quality. package.json ``` { "name": "Design-Rant-Server", "version": "0.1.1", "description": "Server layer between DR Database and Frontend", "main": "server.js", "dependencies": { "async": "*", "body-parser": "~1.0.1", "express": "~4.0.0", "neo4j-driver": "*", "object-checker": "^0.3.24", "validator": "^6.0.0" }, "scripts": { "test": "mocha test", "start": "node server.js" }, "author": "Paras DPain", "license": "MIT", "devDependencies": { "mocha": "^3.1.1" }, "repository": { "type": "git", "url": "git+https://github.com/ParasDPain/DRServer.git" } } ``` server.js ``` /** DR Starts server and API listeners * * Written By: * Paras DPain * * License: * MIT License. All code unless otherwise specified is * Copyright (c) Paras DPain 2016. */ "use strict"; // REQUIRES const express = require("express"); const bodyChecker = require('object-checker').bodyCheckMiddleware; const bodyParser = require("body-parser"); const validator = require('validator'); const db = require('./api.js'); const checkerOptions = require('./checkerOptions.js') // GLOBALS var app = express(); var router = e